Den här snabba handledningen förklarar hur man konverterar en URL till PDF med Python. Den innehåller alla detaljer för att ställa in IDE, en lista med steg och ett exempelkod för att omvandla en länk till PDF med Python. Du kommer att lära dig hur du anpassar utdatafilen genom att ställa in olika parametrar enligt dina behov.
Steg för att Konvertera en Webbsida till PDF med Python
- Ställ in miljön för att använda Aspose.PDF för Python via .NET för att konvertera URL till PDF
- Definiera webbsidans URL och ange HtmlLoadOptions för att anpassa sidinställningarna för utdata
- Skicka en GET-begäran till den angivna URL:en och hämta sidinnehållet som en byte-ström
- Skapa ett dokument i PDF-format från webbsidans ström
- Spara utdata som en PDF-fil
Dessa steg beskriver processen för att konvertera URL till PDF med Python. Ange mål-URL, ställ in utdataformatet för PDF och skicka en GET-begäran för att hämta webbsidans innehåll. Läs in sidans ström i ett dokumentobjekt och spara det slutliga dokumentet som en PDF.
Kod för att Omvandla en Länk till PDF-dokument med 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() |
Denna kod demonstrerar funktionen URL-länk till PDF-konverterare med Python. Ställ in en varningshanterare för att implementera en callback-funktion vid konverteringsfel, en flagga för att rendera innehållet på en enda sida samt inmatningskodning. Du kan läsa in strömmen och skriva ut den genom att avkommentera två rader kod för att inspektera webbsidans innehåll.
Den här artikeln har lärt oss hur man konverterar URL till PDF. För att konvertera HTML-innehåll till PDF, se artikeln hur man konverterar HTML till PDF i Python.