Pretvorite URL u PDF s Pythonom

Ovaj brzi vodič objašnjava kako pretvoriti URL u PDF s Pythonom. Sadrži sve detalje za postavljanje IDE-a, popis koraka i primjer koda za pretvaranje linka u PDF s Pythonom. Naučit ćete kako prilagoditi izlaznu PDF datoteku postavljanjem različitih parametara prema vašim zahtjevima.

Koraci za pretvaranje web stranice u PDF s Pythonom

  1. Postavite okruženje za korištenje Aspose.PDF za Python putem .NET-a za pretvaranje URL-a u PDF
  2. Definirajte URL web stranice i deklarirajte HtmlLoadOptions za prilagodbu postavke izlazne stranice
  3. Pošaljite GET zahtjev na navedeni URL i dobijte sadržaj stranice kao bajt tok
  4. Izradite PDF dokument iz toka web stranice
  5. Spremite izlazni dokument kao PDF datoteku

Ovi koraci opisuju pretvaranje URL-a u PDF s Pythonom. Postavite ciljni URL, opcije izlazne PDF stranice i pošaljite GET zahtjev za dohvaćanje sadržaja web stranice. Učitajte tok web stranice u objekt klase Document i spremite izlazni dokument kao PDF datoteku.

Kod za pretvaranje linka u PDF dokument s Pythonom

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

Ovaj kod pokazuje pretvarač URL linka u PDF s Pythonom. Postavite upozorenje za implementaciju povratne funkcije za pogreške pretvorbe, zastavicu za prikaz sadržaja na jednoj stranici i ulazno kodiranje. Možete pročitati tok i ispisati ga uklanjanjem komentara s dvije linije koda za pregled sadržaja web stranice.

Ovaj članak nas je naučio kako pretvoriti URL u PDF. Za pretvaranje HTML sadržaja u PDF, pogledajte članak kako pretvoriti HTML u PDF u Pythonu.

 Hrvatski