Այս արագ ուղեցույցը բացատրում է, թե ինչպես URL-ը PDF-ի վերածել Python-ով: Այն պարունակում է IDE-ի կարգաբերման բոլոր մանրամասները, քայլերի ցանկ և հղումը PDF-ի վերածելու նմուշային կոդ Python-ով: Դուք կսովորեք, թե ինչպես հարմարեցնել ելքային PDF ֆայլը՝ սահմանելով տարբեր պարամետրեր ձեր պահանջներին համապատասխան:
Քայլեր կայքի էջը PDF-ի վերածելու համար Python-ով
- Սահմանեք միջավայրը Aspose.PDF for Python via .NET-ի օգտագործման համար URL-ը PDF-ի վերածելու համար
- Սահմանեք կայքի էջի URL-ը և հայտարարեք HtmlLoadOptions՝ ելքային էջի կարգաբերման հարմարեցման համար
- Ուղարկեք GET հարցում նշված URL-ին և ստացեք էջի բովանդակությունը բայթային հոսքով
- Ստեղծեք PDF փաստաթուղթ կայքի էջի հոսքից
- Պահպանեք ելքային փաստաթուղթը որպես 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-ով: Սահմանեք զգուշացման մշակիչը փոխարկման սխալների համար հետադարձ կապի ֆունկցիան իրականացնելու համար, դրոշը բովանդակությունը մեկ էջի վրա ցուցադրելու համար և մուտքագրման կոդավորումը: Դուք կարող եք կարդալ հոսքը և տպել այն՝ երկու տող կոդի մեկնաբանությունը հեռացնելով՝ կայքի էջի բովանդակությունը ստուգելու համար:
Այս հոդվածը մեզ սովորեցրեց, թե ինչպես URL-ը PDF-ի վերածել: HTML բովանդակությունը PDF-ի վերածելու համար տե՛ս HTML-ը PDF-ի վերածել Python-ում հոդվածը: