简体   繁体   中英

converting html to pdf

I am trying to convert html to pdf from linux,also i have to use this in web APP please let me know what tools are available for this.Please let me know any other tools for this

So far i have tried

    html2ps htmlfilename > a.ps
    ps2pdf a.ps > a.pdf

But the above doesnt convert images and is ignoring css.My Development environment is linux(RHEL5)

Also i have tried http://www.webupd8.org/2009/11/convert-html-to-pdf-linux.html i get this error

  [root@localhost bin]# ./wkhtmltopdf www.example.com a.pdf
  ./wkhtmltopdf: error while loading shared libraries: libQtWebKit.so.4: cannot open shared object file: No such file or directory

You are on the right path: wkhtmltopdf is the easiest way to do this. Note that the code in the repositories might be outdated (not sure how up-to date this package is); you may need to compile it from source, or get the statically-linked version (which is huge, but has the QT library and other dependencies already included).

Also, in your case, you may just be missing a library - installing libqt4-webkit-dev might do the trick here.

Two ways that are easy to implement and suitable to convert HTML+CSS to pdf are.

1) Using "Jspdf javascript" plugin with "html2canvas plugin" (Web App).

  • Insert stable version of jspdf plugin.

    var script = document.createElement('script'); script.type = 'text/javascript'; script.src ='https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.0.272/jspdf.min.js'; document.head.appendChild(script);

  • Insert html2canvas plugin

    var script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js'; document.head.appendChild(script);

  • Insert the following script

    var html2obj = html2canvas($('your div class here')); var queue = html2obj.parse(); var canvas = html2obj.render(queue);
    var img = canvas.toDataURL("image/jpg"); console.log(img);
    var doc=new jsPDF("p", "mm", "a4"); var width = doc.internal.pageSize.width;
    var height = doc.internal.pageSize.height; doc.addImage(canvas, 'JPEG', 15, 35, 180, 240,'SLOW'); doc.save("save.pdf");

  • Special Case for IE 11

    document.getElementById("your div here").style.backgroundColor = "#FFFFFF";

2) Using wkhtmltopdf

  • Install wkhtmltopdf from here

  • we can directly use wkhtmltopdf from terminal/commandLine, However in case of java language we have a wrapper which we can use.

  • Code Example using wkhtmltopdf wrapper

    import com.github.jhonnymertz.wkhtmltopdf.wrapper.Pdf; import com.github.jhonnymertz.wkhtmltopdf.wrapper.page.PageType; import com.github.jhonnymertz.wkhtmltopdf.wrapper.params.Param; public class PofPortlet extends MVCPortlet {
    @Override public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException
    { super.render(request, response); Pdf pdf = new Pdf();
    pdf.addPage("http://www.google.com", PageType.url); // Add a Table of contents pdf.addToc(); // The "wkhtmltopdf" shell command accepts different types of options such as global, page, headers and footers, and toc. Please see "wkhtmltopdf -H" for a full explanation. // All options are passed as array, for example: // Save the PDF try { pdf.saveAs("E:\\output.pdf"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

3) Other tools include phantom.js, itextpdf, grabz.it

Probably the easiest way would be to launch any modern browser, go to the site, and then use the browser's "print" capability to print to a pdf (assuming your system has a pdf printer set up). I don't know if that's an option in your case, though, and this sort of thing won't work from within a web app. Still, you may want to try it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM