Skip to main content

Wrapper for api2pdf.com library for converting html, urls, and word documents to pdf

Project description

api2pdf.python

Python bindings for Api2Pdf REST API

Api2Pdf.com is a REST API for instantly generating PDF documents from HTML, URLs, Microsoft Office Documents (Word, Excel, PPT), and images. The API also supports merge / concatenation of two or more PDFs. Api2Pdf is a wrapper for popular libraries such as wkhtmltopdf, Headless Chrome, and LibreOffice.

Add a dependency

PyPI

Run the pip command for installing the client pip install api2pdf

Resources

Resources this API supports:

Authorization

Acquire API Key

Create an account at portal.api2pdf.com to get your API key.

Usage

Initialize the Client

All usage starts by calling the import command and initializing the client by passing your API key as a parameter to the constructor.

from api2pdf import Api2Pdf

a2p_client = Api2Pdf('YOUR-API-KEY')

Once you initialize the client, you can make calls like so:

api_response = a2p_client.HeadlessChrome.convert_from_html('<p>Hello, World</p>')
print(api_response.result)

Result Object

An Api2PdfResponse object is returned from every API call. Call the result attribute to retrieve the data. If a call is unsuccessful then success will show False and the error will provide the reason for failure. Additional attributes include the total data usage in, out, and the cost for the API call, typically very small fractions of a penny.

{
    'pdf': 'https://link-to-pdf-only-available-for-24-hours',
    'mbIn': 0.08421039581298828,
    'mbOut': 0.08830547332763672,
    'cost': 0.00017251586914062501,
    'success': True,
    'error': None,
    'responseId': '6e46637a-650d-46d5-af0b-3d7831baccbb'
}

For debugging, you can print the Api2PdfResponse object to see the request and response data.

api_response = a2p_client.HeadlessChrome.convert_from_html('<p>Hello, World</p>')
print(api_response)

Output:

---- API2PDF REQUEST ----
- Headers: {'Authorization': 'YOUR-API-KEY'}
- Endpoint: https://v2018.api2pdf.com/chrome/html
- Payload:
{'html': '<p>Hello, World</p>'}
---- API2PDF RESPONSE ----
{'pdf': 'https://link-to-pdf-only-available-for-24-hours', 'mbIn': 0.08421039581298828, 'mbOut': 0.08830547332763672, 'cost': 0.00017251586914062501, 'success': True, 'error': None, 'responseId': '163c4d25-25d7-4b82-bf50-907597d2ad46'}

wkhtmltopdf

Convert HTML to PDF

api_response = a2p_client.WkHtmlToPdf.convert_from_html('<p>Hello, World</p>')

Convert HTML to PDF (load PDF in browser window and specify a file name)

api_response = a2p_client.WkHtmlToPdf.convert_from_html('<p>Hello, World</p>', inline_pdf=True, file_name='test.pdf')

Convert HTML to PDF (use keyword arguments for advanced wkhtmltopdf settings) View full list of wkhtmltopdf options available.

options = {
    'orientation': 'landscape',
    'pageSize': 'A4'
}
api_response = a2p_client.WkHtmlToPdf.convert_from_html('<p>Hello, World</p>', **options)

Convert URL to PDF

api_response = a2p_client.WkHtmlToPdf.convert_from_url('http://www.api2pdf.com')

Convert URL to PDF (load PDF in browser window and specify a file name)

api_response = a2p_client.WkHtmlToPdf.convert_from_url('http://www.api2pdf.com', inline_pdf=True, file_name='test.pdf')

Convert URL to PDF (use keyword arguments for advanced wkhtmltopdf settings) View full list of wkhtmltopdf options available.

options = {
    'orientation': 'landscape',
    'pageSize': 'A4'
}
api_response = a2p_client.WkHtmlToPdf.convert_from_url('http://www.api2pdf.com', **options)

Headless Chrome

Convert HTML to PDF

api_response = a2p_client.HeadlessChrome.convert_from_html('<p>Hello, World</p>')

Convert HTML to PDF (load PDF in browser window and specify a file name)

api_response = a2p_client.HeadlessChrome.convert_from_html('<p>Hello, World</p>', inline_pdf=True, file_name='test.pdf')

Convert HTML to PDF (use keyword arguments for advanced Headless Chrome settings) View full list of Headless Chrome options available.

options = {
    'landscape': True
}
api_response = a2p_client.HeadlessChrome.convert_from_html('<p>Hello, World</p>', **options)

Convert URL to PDF

api_response = a2p_client.HeadlessChrome.convert_from_url('http://www.api2pdf.com')

Convert URL to PDF (load PDF in browser window and specify a file name)

api_response = a2p_client.HeadlessChrome.convert_from_url('http://www.api2pdf.com', inline_pdf=True, file_name='test.pdf')

Convert URL to PDF (use keyword arguments for advanced Headless Chrome settings) View full list of Headless Chrome options available.

options = {
    'landscape': True
}
api_response = a2p_client.HeadlessChrome.convert_from_url('http://www.api2pdf.com', **options)

LibreOffice

LibreOffice supports the conversion to PDF from the following file formats:

  • doc, docx, xls, xlsx, ppt, pptx, gif, jpg, png, bmp, rtf, txt, html

You must provide a url to the file. Our engine will consume the file at that URL and convert it to the PDF.

Convert Microsoft Office Document or Image to PDF

api_response = a2p_client.LibreOffice.convert_from_url('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx')

Convert Microsoft Office Document or Image to PDF (load PDF in browser window and specify a file name)

api_response = a2p_client.LibreOffice.convert_from_url('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx', inline_pdf=True, file_name='test.pdf')

Merge / Concatenate Two or More PDFs

To use the merge endpoint, supply a list of urls to existing PDFs. The engine will consume all of the PDFs and merge them into a single PDF, in the order in which they were provided in the list.

Merge PDFs from list of URLs to existing PDFs

links_to_pdfs = ['https://LINK-TO-PDF', 'https://LINK-TO-PDF']
merge_result = a2p_client.merge(links_to_pdfs)

Merge PDFs from list of URLs to existing PDFs (load PDF in browser window and specify a file name)

links_to_pdfs = ['https://LINK-TO-PDF', 'https://LINK-TO-PDF']
merge_result = a2p_client.merge(links_to_pdfs, inline_pdf=True, file_name='test.pdf')

Helper Methods

Api2PdfResponse: download_pdf()

On any Api2PdfResponse that succesfully generated a pdf, you can use the handy download_pdf() method to download the pdf to a file-like object which you can then save to your local cache. If the pdf generation was unsuccessful, it will throw a FileNotFoundException.

from api2pdf import Api2Pdf
a2p_client = Api2Pdf('YOUR-API-KEY')

# merge pdfs
links_to_pdfs = ['https://LINK-TO-PDF', 'https://LINK-TO-PDF']
merge_result = a2p_client.merge(links_to_pdfs)

pdf_as_file_object = merge_result.download_pdf()

Delete a PDF on Command with delete(response_id)

By default, Api2Pdf will automatically delete your PDFs after 24 hours. If you have higher security requirements and need to delete the PDFs at-will, you can do so by calling the delete(response_id) method on the Api2Pdf object where response_id parameter comes from the responseId attribute in the Api2PdfResponse result.

from api2pdf import Api2Pdf
a2p_client = Api2Pdf('YOUR-API-KEY')

# generate a pdf
api_response = a2p_client.HeadlessChrome.convert_from_html('<p>Hello World</p>')
response_id = api_response.result['responseId']

# delete the pdf
a2p_client.delete(response_id)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

api2pdf-0.0.11.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

api2pdf-0.0.11-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file api2pdf-0.0.11.tar.gz.

File metadata

  • Download URL: api2pdf-0.0.11.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.6.4

File hashes

Hashes for api2pdf-0.0.11.tar.gz
Algorithm Hash digest
SHA256 a732e0d964660c05388209317b1447e7926f0eb1d2fb18bfaf0317107526c40d
MD5 4a517fc60d49c9d3a8347914adcec7fe
BLAKE2b-256 5f4f79d05a00e94fe6b57d742c35794ffeed78c00f23f7de11e558fc5f304dcd

See more details on using hashes here.

File details

Details for the file api2pdf-0.0.11-py3-none-any.whl.

File metadata

  • Download URL: api2pdf-0.0.11-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.6.4

File hashes

Hashes for api2pdf-0.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 c38b385a9422ccaded2469a01fd5d3119505cf792eb43c00c0f64c2fe7786d4d
MD5 08ead16422389a66cecf1f120d7b0d97
BLAKE2b-256 1a97c9c48ab7bc56232cf562c32a42a0c894215c789ba485318894ccdd977d7f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page