Bu hızlı kılavuz, Python ile URL’yi PDF formatına dönüştürme sürecini açıklar. IDE’nin nasıl ayarlanacağı, adım adım yönergeler ve bağlantıyı Python ile PDF’ye dönüştürme işlemi için örnek kod içerir. Çeşitli parametreleri belirleyerek çıktı PDF dosyasını nasıl özelleştirebileceğinizi öğreneceksiniz.
Python ile Web Sayfasını PDF’ye Dönüştürme Adımları
- Aspose.PDF for Python via .NET kullanım ortamını ayarlayın
- Web sayfasının URL’sini tanımlayın ve çıktı sayfa ayarlarını özelleştirmek için HtmlLoadOptions belirleyin
- Belirtilen URL’ye GET isteği gönderin ve sayfa içeriğini bayt akışı olarak alın
- Web sayfası akışından bir PDF belgesi oluşturun
- Çıktı belgesini PDF dosyası olarak kaydedin
Bu adımlar, Python ile URL’yi PDF’ye dönüştürme sürecini açıklar. Hedef URL’yi belirleyin, çıktı PDF sayfa seçeneklerini ayarlayın ve sayfa içeriğini almak için GET isteği gönderin. Web sayfası akışını Document sınıfı nesnesine yükleyin ve çıktıyı PDF formatında kaydedin.
Python ile Bağlantıyı PDF Belgesine Dönüştürme Kodu
# 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() |
Bu kod, Python ile URL bağlantısını PDF’ye dönüştürme işlemini göstermektedir. Dönüştürme hataları için bir geri çağırma işlevi uygulamak amacıyla uyarı işleyicisini ayarlayın, içeriğin tek bir sayfada işlenmesi için bayrak belirleyin ve giriş kodlamasını yapılandırın. Web sayfası içeriğini kontrol etmek için akışı okuyabilir ve koddaki iki satırın açıklamasını kaldırarak ekrana yazdırabilirsiniz.
Bu makalede, URL’yi PDF’ye dönüştürmeyi öğrendik. HTML içeriğini PDF’ye dönüştürmek için Python’da HTML’den PDF’ye dönüştürme makalesine göz atabilirsiniz.