แปลง URL เป็น PDF ด้วย Python

คู่มือฉบับย่อนี้อธิบาย วิธีแปลง URL เป็น PDF ด้วย Python ซึ่งมีรายละเอียดทั้งหมดเกี่ยวกับการตั้งค่า IDE รายการขั้นตอน และโค้ดตัวอย่างสำหรับการแปลง ลิงก์เป็น PDF ด้วย Python คุณจะได้เรียนรู้วิธีปรับแต่งไฟล์ PDF ที่สร้างขึ้นโดยกำหนดค่าต่างๆ ตามความต้องการของคุณ

ขั้นตอนในการแปลงหน้าเว็บไซต์เป็น PDF ด้วย Python

  1. ตั้งค่าสภาพแวดล้อมเพื่อใช้ Aspose.PDF สำหรับ Python ผ่าน .NET เพื่อแปลง URL เป็น PDF
  2. กำหนด URL ของหน้าเว็บและประกาศ HtmlLoadOptions เพื่อกำหนดค่าการตั้งค่าหน้าสำหรับผลลัพธ์
  3. ส่งคำขอ GET ไปยัง URL ที่ระบุและรับเนื้อหาของหน้าเป็นสตรีมไบต์
  4. สร้าง เอกสาร PDF จากสตรีมของหน้าเว็บ
  5. บันทึกเอกสารที่สร้างขึ้นเป็นไฟล์ PDF

ขั้นตอนเหล่านี้อธิบายกระบวนการ แปลง URL เป็น PDF ด้วย Python ระบุ URL เป้าหมาย กำหนดค่าตัวเลือกของ PDF และส่งคำขอ GET เพื่อดึงเนื้อหาของหน้าเว็บ โหลดสตรีมของหน้าเว็บเข้าสู่วัตถุ Document และบันทึกเป็นเอกสาร PDF

โค้ดสำหรับแปลงลิงก์เป็นเอกสาร PDF ด้วย 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()

โค้ดนี้แสดงให้เห็นถึง ตัวแปลงลิงก์ URL เป็น PDF ด้วย Python ตั้งค่าตัวจัดการคำเตือนเพื่อใช้ฟังก์ชัน callback ในกรณีที่เกิดข้อผิดพลาดระหว่างการแปลง ตั้งค่าธงเพื่อแสดงเนื้อหาในหน้าเดียว และกำหนดการเข้ารหัสอินพุต คุณสามารถอ่านและพิมพ์สตรีมได้โดยยกเลิกการคอมเมนต์สองบรรทัดในโค้ดเพื่อตรวจสอบเนื้อหาของหน้าเว็บ

บทความนี้ได้สอนวิธีแปลง URL เป็น PDF หากคุณต้องการแปลงเนื้อหา HTML เป็น PDF โปรดดูบทความเกี่ยวกับ วิธีแปลง HTML เป็น PDF ใน Python.

 ไทย