close

如何實現html轉換成PDF文件(針對Ubuntu Linux/Debian[其餘os安裝會稍微不同])

  1. You can install wkhtmltopdf using apt-get command:
    $ sudo apt-get install wkhtmltopdf
    $ sudo ln -s /usr/bin/wkhtmltopdf /usr/local/bin/html2pdf
      Sample outputs:
    [sudo] password for vivek:

   Reading package lists... Done

   ………

   Setting up wkhtmltopdf (0.9.9-1) ...

    ………

  1. How do I use wkhtmltopdf

   In this example, convert out bash for loop page to a pdf file:

   $ html2pdf http://www.cyberciti.biz/faq/bash-for-loop/print/             
   /tmp/bash.for.loop.pdf

   Sample outputs:
   Loading pages (1/6)

   Counting pages (2/6)
   …….
   Done

To view generatedpdf file click here. Verify pdf file, enter:

   $ file /tmp/bash.for.loop.pdf

   Sample outputs:

   /tmp/bash.for.loop.pdf: PDF document, version 1.4

  Use the pdfinfo command to prints the contents of the ‘Info’ dictionary (plus     
  some other useful information) from a Portable Document Format (PDF) file:

   $ pdfinfo /tmp/bash.for.loop.pdf

   Sample outputs:

   Title:          Frequently Asked Questions About Linux / UNIX »    

   Bash For Loop Examples » Print
   Creator:
   Producer:       wkhtmltopdf
   ……….
   PDF version:    1.4

3. How do I see all available options?

  You can see a list of commonly used options, enter:
 wkhtmltopdf --help

4.wkhtmltopdf - QXcbConnectionCould not connect to display
(
)參考來源3-留言12


12down voteaccepted

This is a bug, and the fix hasn't been brought to the Debian repositories. Quoting ashkulz (who closed the bug report) :

You're using the version of wkhtmltopdf in the debian repositories, which does not support running headless.

So you can either...

   ()首先第一步驟
 Download wkhtmltopdf from source and compile it (see the instructions in the     
INSTALL.md file
 ; you may remove the 
--recursive option from their git clone line, if you already have Qt 4.8 installed)   

   點選Download wkhtmltopdf from source或是來源4,之後點選INSTALL.md,

   Clone this repository by running the following command:

   git clone --recursive https://github.com/wkhtmltopdf/wkhtmltopdf.git

  ()之後第二步驟
  Run it inside xvfbas suggested by masterkorp in the bug report.,參考留言8

up vote8down vote

There is a more easy way to make John WH Smith solution.

Just install xvfb from apt and then, you can run:

xvfb-run wkhtmltopdf

(just put xvfb-run before any wkhtmltopdf command.)

shareimprove this answer

  安裝完xvfb後注意的地方在於(just put xvfb-run before any wkhtmltopdf command.),讓我們返回步驟二,要改成這樣

$xvfb-run html2pdf http://www.cyberciti.biz/faq/bash-for-loop/print/

  這樣應該就能正常執行了

參考來源:

  1. https://askubuntu.com/questions/320195/how-to-convert-a-html-file-to-pdf-with-colors
  2. http://www.cyberciti.biz/open-source/html-to-pdf-freeware-linux-osx-windows-software/
  3. http://unix.stackexchange.com/questions/192642/wkhtmltopdf-qxcbconnection-could-not-connect-to-display
  4. https://github.com/wkhtmltopdf/wkhtmltopdf/blob/master/INSTALL.md
arrow
arrow
    文章標籤
    html php7 Ubuntu Linux PDF
    全站熱搜

    雨焰 發表在 痞客邦 留言(0) 人氣()