簡體   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

到目前為止我已經嘗試過

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

但是上面沒有轉換圖像並且忽略了 css。我的開發環境是 linux(RHEL5)

我也試過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

您走在正確的道路上: wkhtmltopdf是最簡單的方法。 請注意,存儲庫中的代碼可能已過時(不確定此 package 的最新程度); 您可能需要從源代碼編譯它,或者獲取靜態鏈接版本(它很大,但已經包含 QT 庫和其他依賴項)。

此外,在您的情況下,您可能只是缺少一個庫 - 安裝libqt4-webkit-dev可能會在這里解決問題。

兩種易於實現且適合將 HTML+CSS 轉換為 pdf 的方法是。

1)將“Jspdf javascript”插件與“html2canvas插件”(Web App)一起使用。

  • 插入穩定版的 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);

  • 插入 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);

  • 插入以下腳本

    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");

  • IE 11 的特例

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

2)使用 wkhtmltopdf

  • 這里安裝 wkhtmltopdf

  • 我們可以直接從終端/命令行使用 wkhtmltopdf,但是對於 java 語言,我們有一個可以使用的包裝器

  • 使用 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) 其他工具包括 phantom.js、itextpdf、grabz.it

可能最簡單的方法是啟動任何現代瀏覽器 go 到站點,然后使用瀏覽器的“打印”功能打印到 pdf(假設您的系統有 Z437175BA4191210EE004E1D937 打印機)。 不過,我不知道在你的情況下這是否是一個選項,而且這種事情在 web 應用程序中不起作用。 不過,您可能想嘗試一下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM