Skip to main content

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

Project description

Api2Pdf - Python

Api2Pdf.com | Docs

What is Api2Pdf?

Api2Pdf.com is a service for instantly generating PDFs.

Api2Pdf provides access to wkhtmltopdf, Headless Chrome, and LibreOffice, along with the ability to merge / concatenate PDFs together.

Get Started

This python library provides convenient methods for accessing the REST API documented here.

Acquire API Key

  1. Create an account and login at portal.api2pdf.com
  2. Add a balance to your account (no monthly commitment, sign up with as little as $1)
  3. Create an application and grab your API Key

Install

pip install api2pdf

Usage

from api2pdf import Api2Pdf

a2p = Api2Pdf('YOUR-API-KEY')
api_response = a2p.HeadlessChrome.convert_from_html('<p>Hello, World</p>')
print(api_response.result)

Sample Result

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.

{
    '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 simply print the Api2PdfResponse object to see the request and response data.

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

Output:

---- API2PDF REQUEST ----
- Headers: {'Authorization': 'f8bd6792-f1cd-42df-9bf9-f7a35e59362f'}
- Endpoint: https://v2018.api2pdf.com/merge
- Payload:
['https://LINK-TO-PDF', 'https://LINK-TO-PDF']
---- 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'}

Documentation

Convert HTML to PDF

We support both wkhtmltopdf and Headless Chrome with the endpoint to convert raw html to PDFs. Both endpoints allow you to pass keyword arguments that are options available for their respective libraries.

HeadlessChrome.convert_from_html(html, inline_pdf=False, file_name=None, **options)
WkHtmlToPdf.convert_from_html(html, inline_pdf=False, file_name=None, **options)

Parameters:

html <string> - raw string of html content

inline_pdf <bool> (optional, default=False) - if set to True, web browser will load the PDF in a new tab.

file_name <string> (optional, default=None) - specify an optional file name like "report-03012019.pdf".

**options <keyword args> (optional, default=None) - include options for generating a PDF with your chosen library. See available options here:

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

# headless chrome
headless_chrome_result = a2p.HeadlessChrome.convert_from_html('<p>Hello World</p>')
print(headless_chrome_result.result)

# wkhtmltopdf
wkhtmltopdf_result = a2p.WkHtmlToPdf.convert_from_html('<p>Hello World</p>')
print(wkhtmltopdf_result.result)

Convert URL to PDF

We support both wkhtmltopdf and Headless Chrome with the endpoint to convert urls to PDFs. Both endpoints allow you to pass keyword arguments that are options available for their respective libraries.

HeadlessChrome.convert_from_url(url, inline_pdf=False, file_name=None, **options)
WkHtmlToPdf.convert_from_url(url, inline_pdf=False, file_name=None, **options)

Parameters:

url <string> - url to a pdf that Api2Pdf can consume

inline_pdf <bool> (optional, default=False) - if set to True, web browser will load the PDF in a new tab.

file_name <string> (optional, default=None) - specify an optional file name like "report-03012019.pdf".

**options <keyword args> (optional, default=None) - include options for generating a PDF with your chosen library. See available options here:

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

# headless chrome
headless_chrome_result = a2p.HeadlessChrome.convert_from_url('https://LINK-TO-YOUR-WEBSITE')
print(headless_chrome_result.result)

# wkhtmltopdf
wkhtmltopdf_result = a2p.WkHtmlToPdf.convert_from_url('https://LINK-TO-YOUR-WEBSITE')
print(wkhtmltopdf_result.result)

Convert Microsoft Office Documents and Images to PDF

We use LibreOffice to convert the following formats to PDF:

  • 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.

LibreOffice.convert_from_url(url, inline_pdf=False, file_name=None)

Parameters:

url <string> - url to a file of one of the supported formats above

inline_pdf <bool> (optional, default=False) - if set to True, web browser will load the PDF in a new tab.

file_name <string> (optional, default=None) - specify an optional file name like "report-03012019.pdf".

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

libreoffice_result = a2p.LibreOffice.convert_from_html('https://LINK-TO-YOUR-FILE')
print(libreoffice_result.result)

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(list_of_urls, inline_pdf=False, file_name=None)

Paramaters:

list_of_urls <list> - list of urls to pdfs

inline_pdf <bool> (optional, default=False) - if set to True, web browser will load the PDF in a new tab.

file_name <string> (optional, default=None) - specify an optional file name like "report-03012019.pdf".

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

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

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 = Api2Pdf('YOUR-API-KEY')

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

pdf_as_file_object = merge_result.download_pdf()

FAQ

How do you bill?

$1 will be deducted from your balance every month as long as you maintain an active account. This charge begins 30 days after your first sign up for the service. In addition, we charge $0.001 per megabyte (data in + data out). We require customers to maintain a positive balance on their account to use the service. You can turn off auto-recharge at any time and let your funds run out if you no longer wish to use the service. See our pricing calculator.

Do you offer free accounts?

The average customer spents about $2/month on our product. We do not have free accounts as this time. Feel free to check out alternatives and competitors.

Cancellation and refunds

We do not have any long term contracts. You can leave us at anytime with no further commitments. As our minimum cost is $1.00, we do not provide refunds.

Are there any limits?

Api2Pdf does not set any specific limits on PDF file size, however our system does have processing power limitations. Each PDF request is provided 3 GB of RAM to work with and 110 seconds to generate the PDF. We offer WKHTMLTOPDF, Headless Chrome, and LibreOffice to do conversions. Our platform will have the same limits as those underlying components. If the underlying component fails to convert to PDF, it will also fail via our service. Some examples are:

  • Password protected PDFs
  • Encrypted PDFs
  • HTML that references erroneous content
  • Protected Office Documents

How long are PDFs stored on Api2Pdf.com?

After generating a PDF via the API, you are provided with a link to the file. This link will hold the PDF for 24 hours. If you wish to keep your PDF long term, download the file to your local cache.

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.7.tar.gz (5.9 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.7-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: api2pdf-0.0.7.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for api2pdf-0.0.7.tar.gz
Algorithm Hash digest
SHA256 196a9e2658137ac16441a8519e8063e2e37e6b7e910ad80005ba4683bced636e
MD5 cf1e7ad97651833c11f626dfe97fe01d
BLAKE2b-256 eb14b3696313aade751be442dbebb62d44da959062ff686602716ed848aa0865

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for api2pdf-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 05d61e7148bfa33518e7e78d09d9e3c5a95a41bc139ccf01db668a6fc9aad5e7
MD5 0b127045e1e465019792bfb9c8db5327
BLAKE2b-256 25176481a63da69b5860f97ca1ab72244cd1190d3525936e8eb2cbdcc10a60f6

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