Bài hướng dẫn nhanh này hướng dẫn cách chuyển đổi URL sang PDF bằng Python. Nó bao gồm các bước thiết lập môi trường, danh sách các bước thực hiện và mã mẫu để chuyển đổi liên kết thành PDF bằng Python. Bạn cũng sẽ học cách tùy chỉnh tệp PDF đầu ra bằng cách đặt các tham số khác nhau theo yêu cầu của mình.
Các bước chuyển đổi trang web sang PDF bằng Python
- Thiết lập môi trường để sử dụng Aspose.PDF for Python via .NET để chuyển đổi URL sang PDF
- Xác định URL trang web và khai báo HtmlLoadOptions để tùy chỉnh cài đặt trang đầu ra
- Gửi yêu cầu GET đến URL đã chỉ định và lấy nội dung trang dưới dạng luồng byte
- Tạo tài liệu PDF từ luồng trang web
- Lưu tài liệu đầu ra dưới dạng tệp PDF
Các bước trên mô tả quá trình chuyển đổi URL sang PDF bằng Python. Xác định URL đích, đặt các tùy chọn trang PDF đầu ra và gửi yêu cầu GET để lấy nội dung trang web. Tải luồng trang web vào đối tượng lớp Document và lưu tài liệu đầu ra dưới dạng tệp PDF.
Mã để chuyển đổi liên kết sang tài liệu PDF bằng Python
# Import necessary modules | |
import requests # For making HTTP requests to fetch webpage content | |
from io import BytesIO # To handle byte stream data | |
from aspose.pdf import Document # Import Aspose PDF's Document class for PDF operations | |
import aspose.pdf as ap # Import Aspose PDF module for additional functionality | |
def fetch_web_content_as_stream(webpage_url): | |
""" | |
Fetches the content of a webpage and returns it as a byte stream. | |
Parameters: | |
webpage_url (str): The URL of the webpage to fetch. | |
Returns: | |
BytesIO: A byte stream of the webpage content. | |
""" | |
response = requests.get(webpage_url) # Send GET request to the specified URL | |
response.raise_for_status() # Raise an error if the request fails | |
return BytesIO(response.content) # Return the content as a byte stream | |
def main(): | |
""" | |
Main function that converts a webpage into a PDF document. | |
""" | |
# Set Aspose.PDF license (assumes "license.lic" file is available) | |
license = ap.License() | |
license.set_license("license.lic") | |
# Define the webpage URL to be converted | |
webpage_url = "https://docs.aspose.com/" | |
# Configure HTML-to-PDF conversion options | |
pdf_options = ap.HtmlLoadOptions(webpage_url) # Create HTML load options with the webpage URL | |
pdf_options.page_info.width = 1200 # Set PDF page width | |
pdf_options.page_info.height = 850 # Set PDF page height | |
# Fetch webpage content as a byte stream | |
with fetch_web_content_as_stream(webpage_url) as web_stream: | |
# Uncomment the lines below to print and inspect the webpage content | |
# print(web_stream.read().decode('utf-8', errors='ignore')) | |
# web_stream.seek(0) # Reset the stream position after reading | |
# Create a PDF document from the webpage stream | |
pdf_document = Document(web_stream, pdf_options) | |
# Save the converted PDF document | |
pdf_document.save("Converted_WebPage.pdf") | |
print("URL converted to PDF successfully") | |
# Run the main function if the script is executed directly | |
if __name__ == "__main__": | |
main() |
Mã này minh họa trình chuyển đổi URL sang PDF bằng Python. Thiết lập trình xử lý cảnh báo để triển khai hàm gọi lại khi gặp lỗi chuyển đổi, đặt cờ để hiển thị nội dung trên một trang duy nhất và mã hóa đầu vào. Bạn có thể đọc luồng và in ra màn hình bằng cách bỏ ghi chú hai dòng mã để kiểm tra nội dung trang web.
Bài viết này đã hướng dẫn cách chuyển đổi URL sang PDF. Để tìm hiểu cách chuyển đổi nội dung HTML sang PDF, hãy tham khảo bài viết cách chuyển đổi HTML sang PDF trong Python.