Forum How do I...?

How do I create PDF from javascript

talyrobin
I want to use Prince to create PDFs from my Jama/Velocity/HTML-based reports. I am thinking the easiest way is to add an HTML button to the generated HTML and execute the command line function (prince.exe) to create the PDF. Can I create the PDF via javascript? I've been googling how to exec command line function from javascript, but can't find the correct method. Also is there a better option?
mikeday
It sounds a bit awkward to generate it from JavaScript in the browser. It would be better for the button to trigger a process on the server that generated the HTML.
talyrobin
Thanks for the response. Is there some snippet of code that already does this? I've used the Prince to generate my PDF so I confirmed that it works for me. Now I have to determine how to generate it systematically. I want to keep as simple as possible. Just need to create the PDF and then launch it.

I may be asking a crazy question, but wondering if the html from my browser can be used as input to create the PDF? I can also pass in the url of the html page if that is possible.
mikeday
The main question is what kind of server environment you have, because you can call Prince from Java, or .NET (ASP.NET, C#, or whatever), or PHP, Python, Ruby, and so on.
talyrobin
The OS is Linux. I can create the PDF with Python using this:

createPDF.py...
import subprocess
subprocess.call(["prince.exe","tammy.html","tammy.pdf"]);

But wondering how to call this python script from the click of a button on my HTML report produced from Jama/Velocity.

Edited by talyrobin

mikeday
That's a tricky one. Do you have a web server running? Are there any dynamic pages, CGI scripts, servlets, or other remote code? I'm not really sure what features Jama/Velocity provides.
talyrobin
It uses Apache Tomcat. No other remote code of which I am aware. The tool allows you to upload your velocity file (for reporting) directly into the system. I assume it stores it internally in the db and executes it when requested.
mikeday
Can you upload Java servlet code as well? Or run other tools from it via plugins?
talyrobin
I don't think I can use servlets. Not sure about other plugins. If you let me know the best recommendations, I can work with my sys admin to find out if it is viable.
talyrobin
Any ideas from anyone? I basically have an html page that is generated dynamically. Want to add a button called "ShowPDF". When user clicks, I want to call Prince to generate the PDF from the HTML on the browser and launch it. Does anyone else have a need to do this? I'm hoping I am not the only one. I was thinking I can use AJAX to execute a python script to do this. I am still searching for the syntax and if it's doable.
mikeday
Lots of people do this, and it's really a web server question not a Prince question. It doesn't matter whether you use AJAX from JavaScript or a regular <form> in the HTML, the end result is going to be a HTTP request sent to the server. If you have your Python script on the server, then the missing ingredient is going to be whatever listens for the HTTP request and then invokes the script.

If you already had a web application written in Java, or PHP, or ASP.NET, or Python itself, then it would be simple enough to add another request handler that would invoke Prince.

If you don't currently have a web application there that you can extend, it will be necessary to create one. Now, I read through the introduction to Jama a couple of times and I can't honestly say that I understand what it does. Apparently "Jama gives you and everyone involved a single source of truth", which sounds like something that would be of great interest to philosophers. :D

If you already have Tomcat installed on the server, creating a Java servlet would be a good way to go. I don't think it would need to interact with Jama in any deep way, just take the HTML and pass it to Prince.

Alternatively, you could run another web server like Apache or nginx or even the simple Python HTTP server and then call your Python script from that.

But this really requires talking to your sys admin to see what is the preferred approach that will work well with the software already installed on the server, and that they are willing to support. Once they have indicated which approach is viable, we can help you get it working.
talyrobin
I created the servlet, but am getting the error below. It's been a while since I've coded in Java. Can you please let me know if there is some security I need to grant. I am using Eclipse for the IDE and Apache Tomcat 8.0.


java.io.IOException: Cannot run program "C:\Program Files (x86)\Prince\engine\bin": CreateProcess error=5, Access is denied

OutputStream os_file = new FileOutputStream("C:\\tam.pdf");
Prince p = new Prince("C:\\Program Files (x86)\\Prince\\engine\\bin\\prince");
p.setVerbose(true);
p.setDebug(true);
p.setEmbedFonts(false);
boolean b = p.convert("C:\\tam.html", os_file);
mikeday
Strange that the error refers to "C:\Program Files (x86)\Prince\engine\bin", and not bin\prince, even though it seems to be specifying bin\prince in the source code you pasted?

Assuming the path is correct, you may wish to check for possible Windows UAC issues.

Another more annoying possibility is it could be choking on the space in the command-line, in which case you could try temporarily moving it to C:\Prince and see if that has any effect.

But I would check the path first, as that error message looks very suspicious.