Convertire URL in PDF con Python

Questa breve guida spiega come convertire un URL in PDF con Python. Contiene tutti i dettagli per configurare l’IDE, un elenco di passaggi e un codice di esempio per trasformare un link in PDF con Python. Imparerai a personalizzare il file PDF di output impostando vari parametri in base alle tue esigenze.

Passaggi per Convertire una Pagina Web in PDF con Python

  1. Configurare l’ambiente per utilizzare Aspose.PDF per Python via .NET per convertire URL in PDF
  2. Definire l’URL della pagina web e dichiarare HtmlLoadOptions per personalizzare l’impostazione della pagina di output
  3. Inviare la richiesta GET all’URL specificato e ottenere i contenuti della pagina come flusso di byte
  4. Creare un documento PDF dal flusso della pagina web
  5. Salvare il documento di output come file PDF

Questi passaggi descrivono la conversione di URL in PDF con Python. Imposta l’URL di destinazione, le opzioni della pagina PDF di output e invia la richiesta GET per recuperare i contenuti della pagina web. Carica il flusso della pagina web nell’oggetto della classe Document e salva il documento di output come 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()

Questo codice dimostra il convertitore di link URL in PDF con Python. Imposta il gestore degli avvisi per implementare una funzione di callback per gli errori di conversione, il flag per renderizzare il contenuto su una singola pagina e la codifica di input. Puoi leggere il flusso e stamparlo rimuovendo il commento dalle due righe di codice per ispezionare i contenuti della pagina web.

Questo articolo ci ha insegnato a convertire l’URL in PDF. Per convertire contenuti HTML in PDF, consulta l’articolo su come convertire HTML in PDF con Python.

 Italiano