Xhtml2pdf --css không hoạt động

Bạn đang có nhu cầu muốn chuyển đổi tệp văn bản, html, hay một trang web sang định dạng pdf?

PDF là gì ?

PDF (Portable Document Format) là định dạng tài liệu di động, tập tin văn bản khá phổ biến của hãng Adobe. PDF hỗ trợ các loại phông chữ, định dạng, màu sắc,. PDF có một điểm vượt trội so với Office Word là một văn bản PDF sẽ được hiển thị giống nhau trên các môi trường làm việc khác nhau, có nghĩa là bạn sẽ không mất việc thiếu phông chữ khi mở một tệp pdf trên một tệp.

Chính vì vậy mà càng lúc PDF càng được sử dụng nhiều trong quá trình trao đổi thông tin trên môi trường internet. Càng lúc càng có nhiều loại báo cáo được yêu cầu xuất sang định dạng PDF (thay vì word as before when)

Lựa chọn thư viện chuyển đổi dữ liệu sang PDF

Trong quá trình tìm hiểu việc chuyển dữ liệu sang pdf, tôi đã tìm hiểu các thư viện html2pdf, xhtml2pdf,. Các thư viện này được cái là để áp dụng vào bài toán chuyển đổi dữ liệu thì rất nhanh và nhiều ví dụ. Nhưng khi đưa vào xử lý dữ liệu tiếng Việt có dấu thì. kết quả thu được là ô vuông và dấu ?
Loay hoay với bài toán convert dữ liệu unicode/UTF-8 mãi không được, đến lúc chán chường muốn "buông" thì lại nhớ đến một cái thư . NET is wkhtmltopdf. Đây là một phần mềm mã nguồn mở (theo tiêu chuẩn LGPLv3), cung cấp dòng lệnh giao diện để thực hiện chuyển đổi HTML (hoặc văn bản) sang định dạng PDF, nó hỗ trợ các vấn đề liên quan đến phông chữ Unicode và các phông chữ khác. .
Thật tuyệt là có thư viện pdfkit của Python hỗ trợ đến "tận răng" để kết hợp với wkhtmltopdf.

Các bạn có thể tìm hiểu thêm về thư viện wkhtmltopdf này tại trang chủ. https. //wkhtmltopdf. org/ and pypi of pdfkit. https. //pypi. tổ chức/dự án/pdfkit

Cài đặt wkhtmltopdf

Để bắt đầu cài đặt, chúng ta sẽ đến trang chủ của phần mềm để thực hiện tải xuống. https. //wkhtmltopdf. tổ chức/tải xuống. html, tùy chọn vào hệ điều hành mà chúng ta sẽ tải xuống đúng phiên bản tương ứng. Nguồn mở này hỗ trợ các hệ điều hành. Windows, macOS và hầu hết các bản phân phối của Linux (Debian, Ubuntu, CentOS,. )

Sau khi cài đặt, chúng ta cần thực hiện thiết lập $PATH (CLASS_PATH nếu là Windows) để hệ điều hành trỏ đến đúng vị trí tệp thực thi của phần mềm. Cách thực hiện thiết lập $PATH, các bạn có thể tìm kiếm trên google.
Ví dụ. Tôi đang sử dụng hệ điều hành Windows, tôi cài đặt wkhtmltopdf vào đường dẫn C. \Program Files\wkhtmltopdf, tôi sẽ thực hiện đặt CLASS_PATH cho wkhtmltopdf với đường dẫn. C. \Program Files\wkhtmltopdf\bin
Sau khi cài đặt & thực hiện giữ $PATH xong, bạn thực hiện nhập lệnh kiểm tra xem công việc của chúng ta đã thành công hay chưa bằng cách nào . wkhtmltopdf --version
Kết quả hiện ra. wkhtmltopdf 0. 12. 5 (với qt đã vá) như vậy là đã được thiết lập thành công.

Cài đặt pdfkit

Pdfkit là một thư viện của Python nên chúng ta sẽ cài đặt nó từ "kho" pypi. Để đảm bảo không tạo "rác" trong máy tính, các bạn nên tạo riêng một môi trường ảo (tham khảo hướng dẫn Làm Môi Trường Ảo Python - Môi Trường Lập Trình Ảo ?)

Thực hiện cài đặt thư viện bằng dòng lệnh

def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
9

Kết quả.

def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
0
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
0
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
1
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
2

Sử dụng pdfkit trong chuyển đổi nội dung

1. Thực hiện chuyển đổi một đoạn văn bản thuần sang tệp pdf

bài toán. Yêu cầu thực hiện chuyển đổi các thông báo thu được từ hệ thống cảnh báo sang tệp định dạng PDF để thực hiện gửi vào email tự động cho lực lượng vận động

Để giải quyết yêu cầu bài toán này, chúng ta sẽ sử dụng thư viện pdfkit với câu lệnh sau

import pdfkit

def text2pdf(message, file_name):
    pdfkit.from_string(message, file_name)


if __name__ == "__main__":
    message = "This is a message from codelearn blogger"
    text2pdf(message, "message_en.pdf")

    message = "Đây là thông điệp từ tác giả bài viết trên codelearn"
    text2pdf(message, "message_vi.pdf")

Lưu đoạn mã vào tệp 00_text2pdf. py, after that executed from terminal


def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
3
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
4
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
5
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
6
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
7
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
8
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
9
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
4
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
5
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
6
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
7
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
8
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
9

Ta thu được kết quả

Xhtml2pdf --css không hoạt động

Chữ tiếng Anh thì hiển thị bình thường nhưng tiếng Việt thì bị lỗi font chữ unicode. Để giải quyết vấn đề 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" trước khi thực hiện chuyển đổi

def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)

And here is results

Xhtml2pdf --css không hoạt động

2. Thực hiện chuyển đổi một trang web sang pdf

Ví dụ. Chúng ta cùng chuyển đổi dữ liệu của url https. //www. Google. com/search?q=codelearn. io
Lưu đoạn mã nguồn dưới đây vào tệp 01_url2pdf. p

def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
8

Đứng từ thiết bị đầu cuối, thực hiện chuyển đổi dữ liệu

def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
86
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
4
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
5
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
6
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
7
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
8
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
9

Chúng tôi thu được kết quả tập tin


Xhtml2pdf --css không hoạt động

Đơn giản không đúng. Nhưng đôi khi chúng ta sẽ gặp lỗi liên quan đến việc tải dữ liệu trực tiếp từ iframe hoặc phản hồi của web

Ví dụ. you try to instead of path on by path. https. // codelearning. io/shared/post/quangvinh1986 - danh sách các bài viết của tôi trên codelearn

def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
63

Khi thực hiện chuyển đổi, chúng ta sẽ nhận được Cảnh báo


def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
64
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
65
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
4
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
67
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
68
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
69
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
5
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
6
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
7
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
8
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
9
import pdfkit

def text2pdf(message, file_name):
    pdfkit.from_string(message, file_name)


if __name__ == "__main__":
    message = "This is a message from codelearn blogger"
    text2pdf(message, "message_en.pdf")

    message = "Đây là thông điệp từ tác giả bài viết trên codelearn"
    text2pdf(message, "message_vi.pdf")
85

Và khi mở ra, chúng ta sẽ thu được giao diện bị ẩn một phần dữ liệu

Xhtml2pdf --css không hoạt động

Câu hỏi vui. Các bạn thấy tại sao khi thực hiện convert dữ liệu text thì chúng ta cần thêm đoạn khai báo unicode mà convert cả trang web thì không cần thiết?
Câu trả lời sẽ có ở phần bên dưới.

3. Thực hiện chuyển đổi file dữ liệu html sang file pdf

Request. Thực hiện chuyển đổi nội dung file html sang pdf, yêu cầu giữ nguyên định dạng trên file html như khi hiển thị trên trình duyệt web

Để thực hiện được phần này, chúng ta sẽ tạo ra một tệp html có nội dung như bên dưới và lưu nó lại với cái tên

import pdfkit

def text2pdf(message, file_name):
    pdfkit.from_string(message, file_name)


if __name__ == "__main__":
    message = "This is a message from codelearn blogger"
    text2pdf(message, "message_en.pdf")

    message = "Đây là thông điệp từ tác giả bài viết trên codelearn"
    text2pdf(message, "message_vi.pdf")
86

def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
6

Để đơn giản thì chúng ta sẽ tạo ra tệp 03_html2pdf. py chứa mã thực hiện chuyển đổi sẽ đặt "tạm thời" ở cùng thư mục với bản demo. html,

import pdfkit

def text2pdf(message, file_name):
    pdfkit.from_string(message, file_name)


if __name__ == "__main__":
    message = "This is a message from codelearn blogger"
    text2pdf(message, "message_en.pdf")

    message = "Đây là thông điệp từ tác giả bài viết trên codelearn"
    text2pdf(message, "message_vi.pdf")
8

Sau đó, thực hiện khởi động tệp trên màn hình thiết bị đầu cuối

import pdfkit

def text2pdf(message, file_name):
    pdfkit.from_string(message, file_name)


if __name__ == "__main__":
    message = "This is a message from codelearn blogger"
    text2pdf(message, "message_en.pdf")

    message = "Đây là thông điệp từ tác giả bài viết trên codelearn"
    text2pdf(message, "message_vi.pdf")
87
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
4
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
5
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
6
_______07
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
8
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
9

Thu được kết quả tập tin

Xhtml2pdf --css không hoạt động

Lại một lần nữa phông chữ tiếng Việt lại làm phiền chúng ta. Quay lại câu hỏi phía trên của tôi, tại sao các trang web mà chúng tôi thực hiện chuyển đổi lại không bị lỗi phông chữ mà cái tệp html bé tẹo phía trên lại bị lỗi phông chữ ?
Câu trả lời . Một cái có thẻ

set in the tag and one cái thì không có

Bây giờ chúng ta chúng ta thực hiện thêm đoạn thẻ

trên file html và thực hiện chạy lại file convert. Kết quả thu được

Xhtml2pdf --css không hoạt động

Kết quả thì dữ liệu unicode đã được hiển thị thành công nhưng tốt nhất là chúng ta vẫn nên thêm vào các khai báo cấu hình liên quan đến unicode khi thực hiện convert file

def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
9

Ngoài việc hỗ trợ mã hóa UTF-8, pdfkit còn hỗ trợ nhiều loại cấu hình tham số khác nhau. Chúng ta sẽ tìm hiểu nó trong phần tiếp theo

Một số lưu ý khi sử dụng pdfkit và wkhtmltopdf

1. Các loại tham số cấu hình mà pdfkit hỗ trợ

Đầu tiên là cấu hình giống như khi chúng ta thực hiện in ấn một văn bản ra máy trong văn phòng

def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
0

kích thước trang. Khổ giấy của file pdf sau khi convert. Mặc định là A4, có thể thay đổi cách đánh giá Letter, A5,

định hướng. Mặc định là khổ xoay dọc (Chân dung), có thể thay đổi sang xoay ngang (Phong cảnh)

im lặng. Hiển thị nhật ký trong quá trình chuyển đổi sang pdf. Giá trị tương ứng với các mức đặt nhật ký (INFO, WARNING,. ). Thường đặt '' trên sản xuất để tránh lỗi hiển thị thiết bị đầu cuối

dpi. Dấu chấm trên mỗi inch - Độ đậm nét của ký tự trên tệp pdf. Default is 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 lượng dpi lên được

lề. Giãn cách từ các vị trí của các màn đối ứng đến phần dữ liệu đầu tiên của nội dung của tệp. Tùy theo yêu cầu mà chúng ta thay đổi hoặc giữ nguyên mặc định

Do pdfkit thực hiện dựa trên thư viện gốc wkhtmltopdf nên các tham số cấu hình của wkhtmltopdf cũng có thể sử dụng với pdfkit (ví dụ:. bật/tắt css, javascript,. ), Các tham số này, các bạn có thể tham khảo thêm từ trợ giúp của wkhtmltopdf

def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
94

2. 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 đặt $PATH trên hệ thống hoặc hệ thống bị xóa mất CLASS_PATH, khi thực hiện các lệnh chuyển đổi sẽ đưa ra một số cảnh báo

______495

def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
96

Nếu bạn vẫn muốn sử dụng pdfkit, thì pdfkit sẽ cung cấp một số tham số để bạn thực hiện con trỏ đường dẫn đến vị trí cài đặt wkhtmltopdf

Đầu tiên thì phải tìm đường dẫn đến file chạy wkhtmltopdf trên hệ điều hành (exe trên windows hoặc file class trên UNIX)

Trên Windows, chúng ta chỉ cần thực hiện tìm kiếm wkhtmltopdf. exe is ra file and location file.
Trên UNIX, chúng ta sử dụng dòng lệnh.

def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
97

Thường thì đường dẫn sẽ được lưu ở.

def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
98

Thực hiện sao chép dữ liệu PATH trên và đưa vào tham số cấu hình

def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
99
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
00
def text2pdf(message, file_name):
    options = {'encoding': "UTF-8"}
    pdfkit.from_string(message, file_name, options=options)
01

3. Thực hiện chuyển nội dung nhiều trang web sang một tệp pdf

Thư viện pdfkit cho phép chúng tôi thực hiện "ghép" nội dung nhiều trang vào một tệp pdf duy nhất như ví dụ dưới đây