Hướng dẫn wkhtmltopdf path - đường dẫn wkhtmltopdf

Project description

This package installs a wkhtmltopdf binary in a Heroku or cloudControl deployment. Versioning on this package mimics the version number of the included wkhtmltopdf binary.

Nội dung chính

  • Project description
  • Installation
  • Acknowledgements
  • Download files
  • Source Distribution
  • Dẫn nhập
  • Lựa chọn pdfkit ?
  • Cài đặt thư viện
  • Cài đặt wkhtmltopdf
  • Cài đặt pdfkit.
  • Sử dụng pdfkit với Python
  • Sử dụng các các chức năng mặc định.
  • Convert một trang web sang pdf
  • Convert một file html sang pdf
  • Convert một đoạn text sang pdf
  • Thêm một chút nâng cao với pdfkit
  • Thay đổi các cấu hình mặc định.
  • Xử lý trường hợp không có quyền thiết lập $path
  • Convert nhiều nội dung và chung một file.

wkhtmltopdf is an open source (LGPLv3) command line tool to render HTML into PDF using the Qt WebKit rendering engine.

Installation

Acknowledgements

Download files

Source Distribution

Dẫn nhập

Lựa chọn pdfkit ?

Acknowledgements

Download files

Download files

Source Distribution

Source Distribution

Dẫn nhập

Nội dung chính

  • Dẫn nhập
  • Lựa chọn pdfkit ?
  • Cài đặt thư viện
  • Cài đặt wkhtmltopdf
  • Cài đặt pdfkit.
  • Sử dụng pdfkit với Python
  • Sử dụng các các chức năng mặc định.
  • Convert một trang web sang pdf
  • Convert một file html sang pdf
  • Convert một đoạn text sang pdf
  • Thêm một chút nâng cao với pdfkit
  • Thay đổi các cấu hình mặc định.
  • Xử lý trường hợp không có quyền thiết lập $path
  • Convert nhiều nội dung và chung một file.

wkhtmltopdf is an open source (LGPLv3) command line tool to render HTML into PDF using the Qt WebKit rendering engine.

Install it in the usual way:

pip install wkhtmltopdf-packFeb 4, 2012 at 21:50

UsageAmit

There are multiple Python wrappers for wkhtmltopdf you can use. If you are using Django in your project, you can use the django-wkhtmltopdf wrapper.1 gold badge2 silver badges7 bronze badges

The binary you want to use is named wkhtmltopdf-pack.

Binaries are originally generated for the wkhtmltopdf-heroku Ruby Gem. A big thank you to the wkhtmltopdf-heroku contributors!Aug 1, 2012 at 12:58

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.stuudent

I'm trying to convert some HTML files stored locally on my computer to PDF format through a Python script, and I've tried xhtml2pdf but I ran into countless errors and decided to stop using it. 4 silver badges11 bronze badges

I heard that wkhtmltopdf was a better alternative and I found a Python package that integrated well into it. Unfortunately, this package requires xvfb which cannot be installed for Windows. Is there any other way to install wkhtmltopdf for Python on Windows?

  • Thanks for your help!
  • asked Feb 4, 2012 at 21:50

    def html_to_pdf(<html>):
       with tempfile.NamedTemporaryFile(suffix='.html', delete=False) as temp:
           temp.write(html.encode('windows-1252'))
           temp.seek(0)
           pdfkit.from_file(temp.name, 'out.pdf') 
           temp.close()
           pdf = open('out.pdf', 'r+b')
           response = HttpResponse(pdf.read(), content_type='application/pdf')
           response['Content-Disposition'] = 'attachment; filename=out.pdf'
           pdf.close()
           # remove the locally created pdf file.
           os.remove(temp.name)
           return response
    

AmitAmitMar 27, 2020 at 13:20

Dẫn nhập

Lựa chọn pdfkit ?

Lựa chọn pdfkit ?

Cài đặt thư viện

Cài đặt wkhtmltopdf

Cài đặt thư viện

Cài đặt wkhtmltopdf

Cài đặt wkhtmltopdf

Cài đặt pdfkit.

Sử dụng pdfkit với Python

Sử dụng các các chức năng mặc định.

pip install pdfkit
7

Convert một trang web sang pdf

Convert một file html sang pdf

Convert một đoạn text sang pdf

Cài đặt pdfkit.

Sử dụng pdfkit với Python

pip install pdfkit

Kết quả hiện ra:

Collecting pdfkit
  Downloading pdfkit-0.6.1-py3-none-any.whl (12 kB)
Installing collected packages: pdfkit
Successfully installed pdfkit-0.6.1

Sử dụng pdfkit với Python

Thư viện pdfkit cung cấp khá đầy đủ các sample, các bạn có thể tham khảo thêm tại: https://pypi.org/project/pdfkit/

Sử dụng các các chức năng mặc định.

Convert một trang web sang pdf

Mở terminal, di chuyển đến một thư mục ... "dễ nhớ"và thực hiện một số dòng lệnh sau: (Nhớ chuyển từ terminal sang mode python bằng cách gõ

pip install pdfkit
8 trước nhé)

>>> import pdfkit
>>> pdfkit.from_url("https://viblo.asia/newest", "url2pdf.pdf")

Sau đó đợi thư viện làm việc và phản hồi lại.

Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
True

Như này là kết quả đã thành công, mở thư mục vừa di chuyển đến, ta sẽ thấy file url2pdf.pdf

Convert một file html sang pdf

Trước tiên, thực hiện tạo ra một file .html và đặt nó vào một thư mục, trong ví dụ này, tôi dùng file có tên

pip install pdfkit
9.

Mở terminal và thực hiện di chuyển đến thưc mục chứa file

pip install pdfkit
9, sau đó chuyển sang mode nhập code python

>>> import pdfkit
>>> pdfkit.from_file('report.html', 'report.pdf')

Chờ một chút để hệ thống xử lý và đưa ra thông báo.

Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
True

Sau đó ta có thể mở file report.pdf để xem kết quả convert:

Lưu ý: để có thể convert đúng dữ liệu unicode thì là file .html phải có thẻ

Collecting pdfkit
  Downloading pdfkit-0.6.1-py3-none-any.whl (12 kB)
Installing collected packages: pdfkit
Successfully installed pdfkit-0.6.1
1 với nội dung như bên dưới đặt tại thẻ
Collecting pdfkit
  Downloading pdfkit-0.6.1-py3-none-any.whl (12 kB)
Installing collected packages: pdfkit
Successfully installed pdfkit-0.6.1
2

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Convert một đoạn text sang pdf

>>> import pdfkit
>>>  pdfkit.from_string("convert dữ liệu string sang pdf", "string.pdf")

Chờ một chút để hệ thống xử lý và đưa ra thông báo.

Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
True

Sau đó ta có thể mở file report.pdf để xem kết quả convert:

Lưu ý: để có thể convert đúng dữ liệu unicode thì là file .html phải có thẻ

Collecting pdfkit
  Downloading pdfkit-0.6.1-py3-none-any.whl (12 kB)
Installing collected packages: pdfkit
Successfully installed pdfkit-0.6.1
1 với nội dung như bên dưới đặt tại thẻ
Collecting pdfkit
  Downloading pdfkit-0.6.1-py3-none-any.whl (12 kB)
Installing collected packages: pdfkit
Successfully installed pdfkit-0.6.1
2

pip install pdfkit
0

Convert một đoạn text sang pdf

Sau đó ta có thể mở file string.pdf để xem kết quả convert --> Lỗi font chữ unicode

Để giải quyết việc này, pdfkit cho phép các bạn thực hiện khai báo thêm một số "cấu hình".

Kết quả thu được:

pdfkit cung cấp khá nhiều các tham số cấu hình khác dựa trên bộ tham số của wkhtmltopdf. Chúng ta sẽ làm quen với một số tham số ở phần tiếp theo.

pip install pdfkit
1

Thêm một chút nâng cao với pdfkit

Thay đổi các cấu hình mặc định.

Pdfkit cung cấp việc thay đổi một số tham số cầu hình thường gặp giống như lúc chúng ta cấu hình in một văn bản ra máy in văn phòng:

Collecting pdfkit
  Downloading pdfkit-0.6.1-py3-none-any.whl (12 kB)
Installing collected packages: pdfkit
Successfully installed pdfkit-0.6.1
3: Khổ giấy của file pdf sau khi convert. Mặc định là A4, có thể thay đổi thành cách giá trị Letter, A5,...

Collecting pdfkit
  Downloading pdfkit-0.6.1-py3-none-any.whl (12 kB)
Installing collected packages: pdfkit
Successfully installed pdfkit-0.6.1
4: Mặc định là khổ giấy xoay dọc (Portrait), có thể thay đổi sang xoay ngang (Landscape)

Collecting pdfkit
  Downloading pdfkit-0.6.1-py3-none-any.whl (12 kB)
Installing collected packages: pdfkit
Successfully installed pdfkit-0.6.1
5: Hiển thị log trong quá trình convert sang pdf. Giá trị ưng với các level đặt log (INFO, WARNING,...). Thường đặt '' trên production để tránh lỗi hiển thị terminal.

pip install pdfkit
2

Collecting pdfkit Downloading pdfkit-0.6.1-py3-none-any.whl (12 kB) Installing collected packages: pdfkit Successfully installed pdfkit-0.6.1 6: Dots Per Inch - Độ đậm nét của ký tự trên file pdf. Mặc định là 96. Tham số này không nên thay đổi. Với Windows, UNIX thì wkhtmltopdf sẽ reset về 96pdi, nếu chuyển qua convert trên máy OSX thì mới có thể tăng số lượng dpi lên được.

Collecting pdfkit
  Downloading pdfkit-0.6.1-py3-none-any.whl (12 kB)
Installing collected packages: pdfkit
Successfully installed pdfkit-0.6.1
7: Giãn cách từ vị trí các mép tương ứng đến phần dữ liệu đầu tiên của nội dung của file. Tùy theo nhu cầu mà chúng ta thay đổi hoặc giữ nguyên mặc định.

Các tham số khác (enable/disable css, javascript, ...), các bạn có thể tham khảo thêm từ help của wkhtmltopdf

pip install pdfkit
3

Xử lý trường hợp không có quyền thiết lập $path

Đôi khi bạn không có quyền thực hiện set $PATH trên hệ thống mà vẫn muốn dùng luôn pdfkit, pdfkit sẽ cung cấp một tham số để bạn thực hiện trỏ đường dẫn đến vị trí cài đặt wkhtmltopdf.

pip install pdfkit
4

Để xem đường dẫn của thư viện wkhtmltopdf, trên Unix, bạn gõ

/usr/local/bin/wkhtmltopdf

Trên windows thì dùng tính năng search để tìm xem file wkhtmltopdf.exe ở đâu. Sao chép đường

Việc trỏ config trực tiếp này sẽ giúp chúng ta ignore được các lỗi:

pip install pdfkit
5

IOError: 'No wkhtmltopdf executable found' OSError: 'No wkhtmltopdf executable found'


Convert nhiều nội dung và chung một file.