Python के साथ URL को PDF में बदलें

यह त्वरित ट्यूटोरियल बताता है कि Python के साथ URL को PDF में कैसे बदलें। इसमें IDE सेट करने, चरणों की सूची और Python के साथ लिंक को PDF में बदलने के लिए एक नमूना कोड शामिल है। आप अपनी आवश्यकताओं के अनुसार विभिन्न पैरामीटर्स सेट करके आउटपुट PDF फ़ाइल को कस्टमाइज़ करना सीखेंगे।

Python के साथ वेब पेज को PDF में बदलने के चरण

  1. URL को PDF में बदलने के लिए Aspose.PDF for Python via .NET का उपयोग करने के लिए पर्यावरण सेट करें
  2. वेब पेज URL को परिभाषित करें और आउटपुट पेज सेटअप को कस्टमाइज़ करने के लिए HtmlLoadOptions घोषित करें
  3. निर्दिष्ट URL पर GET अनुरोध भेजें और पेज सामग्री को बाइट स्ट्रीम के रूप में प्राप्त करें
  4. वेब पेज स्ट्रीम से PDF दस्तावेज़ बनाएं
  5. आउटपुट दस्तावेज़ को PDF फ़ाइल के रूप में सहेजें

ये चरण Python के साथ URL को PDF में बदलने का वर्णन करते हैं। लक्ष्य URL, आउटपुट PDF पेज विकल्प सेट करें और वेब पेज सामग्री प्राप्त करने के लिए GET अनुरोध भेजें। वेब पेज स्ट्रीम को Document क्लास ऑब्जेक्ट में लोड करें और आउटपुट दस्तावेज़ को PDF फ़ाइल के रूप में सहेजें।

Python के साथ लिंक को 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()

यह कोड Python के साथ URL लिंक को PDF कन्वर्टर में बदलने को दर्शाता है। कनवर्ज़न त्रुटियों के लिए कॉलबैक फ़ंक्शन को लागू करने के लिए चेतावनी हैंडलर सेट करें, सामग्री को एक पेज पर रेंडर करने के लिए फ्लैग और इनपुट एन्कोडिंग। आप स्ट्रीम को पढ़ सकते हैं और वेब पेज सामग्री का निरीक्षण करने के लिए कोड की दो लाइनों को अनकमेंट करके इसे प्रिंट कर सकते हैं।

इस लेख ने हमें URL को PDF में बदलना सिखाया है। HTML सामग्री को PDF में बदलने के लिए, Python में HTML को PDF में कैसे बदलें लेख देखें।

 हिन्दी