简体   繁体   English

将 html 转换为 pdf

[英]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 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)但是上面没有转换图像并且忽略了 css。我的开发环境是 linux(RHEL5)

Also i have tried http://www.webupd8.org/2009/11/convert-html-to-pdf-linux.html i get this error我也试过http://www.webupd8.org/2009/11/convert-html-to-pdf-linux.html我得到这个错误

  [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.您走在正确的道路上: wkhtmltopdf是最简单的方法。 Note that the code in the repositories might be outdated (not sure how up-to date this package is);请注意,存储库中的代码可能已过时(不确定此 package 的最新程度); 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).您可能需要从源代码编译它,或者获取静态链接版本(它很大,但已经包含 QT 库和其他依赖项)。

Also, in your case, you may just be missing a library - installing libqt4-webkit-dev might do the trick here.此外,在您的情况下,您可能只是缺少一个库 - 安装libqt4-webkit-dev可能会在这里解决问题。

Two ways that are easy to implement and suitable to convert HTML+CSS to pdf are.两种易于实现且适合将 HTML+CSS 转换为 pdf 的方法是。

1) Using "Jspdf javascript" plugin with "html2canvas plugin" (Web App). 1)将“Jspdf javascript”插件与“html2canvas插件”(Web App)一起使用。

  • Insert stable version of jspdf plugin.插入稳定版的 jspdf 插件。

    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插入 html2canvas 插件

    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 IE 11 的特例

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

2) Using wkhtmltopdf 2)使用 wkhtmltopdf

  • Install wkhtmltopdf from here这里安装 wkhtmltopdf

  • we can directly use wkhtmltopdf from terminal/commandLine, However in case of java language we have a wrapper which we can use.我们可以直接从终端/命令行使用 wkhtmltopdf,但是对于 java 语言,我们有一个可以使用的包装器

  • Code Example using wkhtmltopdf wrapper使用 wkhtmltopdf 包装器的代码示例

    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 3) 其他工具包括 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).可能最简单的方法是启动任何现代浏览器 go 到站点,然后使用浏览器的“打印”功能打印到 pdf(假设您的系统有 Z437175BA4191210EE004E1D937 打印机)。 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.不过,我不知道在你的情况下这是否是一个选项,而且这种事情在 web 应用程序中不起作用。 Still, you may want to try it.不过,您可能想尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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