15

I want to convert HTML (containing JavaScript ) to a PDF. How can I do that?

I just want to show what is being shown in web page. I am displaying a gantt chart that is generated by a JavaScript library.

Now I want to save that HTML web page as a PDF, how to do that?

Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
g.revolution
  • 11,962
  • 23
  • 81
  • 107
  • 10
    Java is completely different from Javascript! – Ikke Nov 06 '09 at 08:50
  • 1
    i know , i want solution using java or javascript. both are acceptable for me. – g.revolution Nov 06 '09 at 08:55
  • Some questions: Is Java already present in the project? How Javascript does this chart (images, table, ...) and where the data come from? I think your question won't have a direct solution. – sinuhepop Nov 06 '09 at 09:04

7 Answers7

5

We are also looking for some way to convert html files with complex javascript to pdf. The javasript in our files contains document.write and DOM manipulation.

We have tried using a combination of HtmlUnit to parse the files and Flying Saucer to render to pdf but the results are not satisfactory enough. It works, but in our case the pdf is not close enough to what the user wants.

If you want to try this out, here is a code snippet to convert a local html file to pdf.

URL url = new File("test.html").toURI().toURL();
WebClient webClient = new WebClient(); 
HtmlPage page = webClient.getPage(url);

OutputStream os = null;
try{
   os = new FileOutputStream("test.pdf");

   ITextRenderer renderer = new ITextRenderer();
   renderer.setDocument(page,url.toString());
   renderer.layout();
   renderer.createPDF(os);
} finally{
   if(os != null) os.close();
}
Serxipc
  • 6,639
  • 9
  • 40
  • 51
2

I'm surprised no one mentioned the possibility to use an API to do the work.

Granted, if you want to stay secure, converting HTML to PDF directly from within the browser using javascript is not a good idea.

But here's what you can do:

When your user hit the "Print" (for example) button, you:

  1. Send a request to your server at a specific endpoint with details about what to convert (URL of the page for instance).
  2. This endpoint will then send the data to convert to an API, and will receive the PDF in response
  3. which it will return to your user.

For a user point of view, they will receive a PDF by clicking on a button.

There are many available API that does the job, some better than others (that's not why I'm here) and a Google search will give you a lot of answers.

Depending on what is written your backend, you might be interested in PDFShift (Truth: I work there).

They offer ready to work packages for PHP, Python and Node.js. All you have to do is install the package, create an account, indicate your API key and you are all set!

The advantage of the API is that they work well in all languages. All you have to do is a request (generally POST) containing the data you want to be converted and get a PDF back. And depending on your usage, it's generally free, except if you are a heavy user.

Cyril N.
  • 38,875
  • 36
  • 142
  • 243
0

Using the browser's Print... menu item, you can utilize a PDF Printer Driver, like PDFCreator. This way any JavaScript included in the page is processed by the browser when the page is rendered.

PDFCreator is a free tool to create PDF files from nearly any Windows application.

  • Create PDFs from any program that is able to print
Community
  • 1
  • 1
gimel
  • 83,368
  • 10
  • 76
  • 104
0

With Docmosis or JODReports you could feed your HTML and Javascript to the document render process which could produce PDF or doc or other formats. The conversion underneath is performed by OpenOffice so results will be dependent on the OpenOffice import filters. You can try manually by saving your web page to a file, then loading with OpenOffice - if that looks good enough, then these tools will be able to give you the same result as a PDF.

Paul Jowett
  • 6,513
  • 2
  • 24
  • 19
0

Check this out http://www.techumber.com/2015/04/html-to-pdf-conversion-using-javascript.html

Basically you need to use html2canvas and jspdf to make it work. First you will convert your dom to image and then you will use jspdf to create pdf with the images.

EDIT: A short note on how it work. We will use two libraries to make this job done. http://html2canvas.hertzen.com/ and https://github.com/MrRio/jsPDF First we will create a dom image by using html2canvas them we will use jspdf addImage method to add that image to pdf. It seems simple but there are few bugs in jsPdf and html2cavas so you may need to change dom style temporarily. Hope this helps.

Dimpu Aravind Buddha
  • 9,505
  • 4
  • 17
  • 23
-3

You can do it using a jquery,

Use this code to link the button...

$(document).ready(function() {
    $("#button_id").click(function() {
        window.print();
        return false;
    });
});

This link may be also helpful: jQuery Print HTML Pdf Page Options Link

Dusman
  • 122
  • 4
  • 11
  • Could you explain in your answer how this is a solution for saving an HTML page as PDF? The Asker did not say anything about printing... – trincot Nov 29 '15 at 23:14
  • "10 jQuery Print Page Options" is only a link name, this plugin will give HTML page as PDF..,however there is a button in plugin which name is "PRINT",but it will give a pdf file.. – Dusman Dec 30 '15 at 09:53
  • OK, would be nice to add this information in your answer, as it is quite surprising to read *Use this code to link the print button...* when the Asker did not mention a print button at all. – trincot Dec 30 '15 at 09:58
  • 2
    This isn't even using a jquery plugin. This is using standard jquery to wait until the dom loads and then bind a click event that invokes the native window function `print()`. - 10,000 – mwilson Dec 07 '17 at 01:59
  • I know this is an old post, but it's a prime example why "link-only"-answers are bad. The link doesn't work any more so all this answer says is basically "trigger the print dialog to show" which is quite different from what the question was. – M. Eriksson Jul 24 '19 at 07:45
-3

Copy and paste this in your site to provide a link which will convert the page to a PDF page.

<a href="javascript:void(window.open('http://www.htmltopdfconverter.net/?convert='+window.location))">Convert To PDF</a>
Veger
  • 37,240
  • 11
  • 105
  • 116
Jokin
  • 11
  • 2