将 URL 转换为 PDF(Python)

本快速指南介绍了如何 使用 Python 将 URL 转换为 PDF。包括环境设置、步骤列表以及示例代码,帮助您完成 链接转 PDF(Python)。此外,您还将学习如何根据需求自定义输出 PDF 文件。

使用 Python 将网页转换为 PDF 的步骤

  1. 设置环境以使用 Aspose.PDF for Python via .NET 将 URL 转换为 PDF
  2. 定义网页 URL 并声明 HtmlLoadOptions 以自定义输出页面设置
  3. 发送 GET 请求到指定 URL 并以字节流获取网页内容
  4. 从网页流创建 PDF 文档
  5. 将输出文档保存为 PDF 文件

以上步骤详细说明了 如何使用 Python 将 URL 转换为 PDF。设置目标 URL、自定义 PDF 页面选项,并发送 GET 请求以获取网页内容。然后,将网页流加载到 Document 类对象并将其保存为 PDF 文件。

使用 Python 将链接转换为 PDF 文档的示例代码

# 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()

此代码演示了 Python URL 转 PDF 转换器。其中包括设置警告处理程序以实现回调函数处理转换错误,启用单页渲染选项,以及设置输入编码。您可以取消注释代码中的两行,以读取流并打印网页内容进行调试。

本文介绍了如何将 URL 转换为 PDF。如果您需要将 HTML 内容转换为 PDF,请参考 如何在 Python 中将 HTML 转换为 PDF

 简体中文