Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert HTML to PDF using a REST API in Python
You can easily convert HTML files to PDF documents using Python on the cloud. It can help you keep records or share HTML files in a portable form. As a Python developer, you can convert HTML to PDF documents programmatically. Here, you will learn how to convert HTML to PDF documents using a REST API in Python.
The following topics shall be covered in this article:
1. Configure REST API
2. Upload HTML File
3. Convert HTML to PDF using REST API in Python
4. Convert HTML to PDF and Add Watermark
5. Download the Converted PDF File
6. HTML to PDF Conversion without using Cloud Storage
7. Convert HTML to PDF and Download Directly
client_id = "659fe7da-715b-4744-a0f7-cf469a392b73"
client_secret = "b377c36cfa28fa69960ebac6b6e36421"
configuration = groupdocs_conversion_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
my_storage = ""
# Create necessary API instances
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Prepare convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "sample.html"
settings.format = "pdf"
settings.output_path = "converted"
# PDF convert options
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.center_window = True # Set position of the document's window on the screen
convertOptions.display_doc_title = True
convertOptions.dpi = 1024.0
convertOptions.fit_window = False
convertOptions.from_page = 1 # Start conversion from FromPage page
convertOptions.linearize = False # Linearize PDF Document for the Web
convertOptions.margin_top = 5
convertOptions.margin_left = 5
convertOptions.unembed_fonts = True # Make fonts not embedded
convertOptions.remove_pdfa_compliance = False # Remove Pdf-A Compliance
settings.convert_options = convertOptions
# Convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# convert
result = convert_api.convert_document(request)
# Done
print("Document converted: " + result[0].path)
# Create necessary API instances
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Prepare convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "sample.html"
settings.format = "pdf"
settings.output_path = None # leave OutputPath will result the output as document IOStream
# Prepare request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert
response = convert_api.convert_document_download(request)
# Move downloaded file to your working directory
shutil.move(response, "C:\\Files\\")
# api initialization
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "sample.html"
settings.format = "pdf"
settings.output_path = "converted"
# define watermark options
watermark = groupdocs_conversion_cloud.WatermarkOptions()
watermark.text = "THIS IS SAMPLE WATERMARK"
watermark.bold = True;
watermark.font_size = 34;
watermark.color = "Gray";
watermark.background = False;
watermark.rotation_angle = 30;
watermark.left = 90;
watermark.top = 500;
# define PDF convert options
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.watermark_options = watermark
settings.convert_options = convertOptions
# create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# convert
result = convert_api.convert_document(request)
print("Document converted successfully: " + result[0].url)
# API initialization
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# create download file request
request = groupdocs_conversion_cloud.DownloadFileRequest("converted\\sample.pdf", my_storage)
# download file
response = file_api.download_file(request)
# Move downloaded file to your working directory
shutil.move(response, "C:\\Files\\")
# create instance of the API
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# upload file request
request = groupdocs_conversion_cloud.UploadFileRequest("sample.html", "C:\\Files\\sample.html", my_storage)
# upload sample file
response = file_api.upload_file(request)
# Create necessary API instances
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Prepare request
request = groupdocs_conversion_cloud.ConvertDocumentDirectRequest("pdf", "C:\\Files\\sample.html")
# Convert
result = convert_api.convert_document_direct(request)
# Move downloaded file to your working directory
shutil.move(result, "C:\\Files\\")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment