This client library is a wrapper for the Api2Pdf.com REST API. See full REST api documentation at https://www.api2pdf.com/documentation/v2. Api2Pdf is a powerful API that supports HTML to PDF, URL to PDF, HTML to Image, URL to Image, Thumbnail / image preview of an Office file, Office files (Word to PDF), HTML to Docx, HTML to excel, PDF to HTML, merge PDFs together, add bookmarks to PDFs, add passwords to PDFs
Project description
api2pdf.python
Python bindings for Api2Pdf REST API
Api2Pdf.com is a powerful REST API for instantly generating PDF and Office documents from HTML, URLs, Microsoft Office Documents (Word, Excel, PPT), Email files, and images. You can generate image preview or thumbnail of a PDF, office document, or email file. The API also supports merge / concatenation of two or more PDFs, setting passwords on PDFs, adding bookmarks to PDFs, splitting or extracting pages from a PDF. Api2Pdf is a wrapper for popular libraries such as wkhtmltopdf, Headless Chrome, PdfSharp, 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.Chrome.html_to_pdf('<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.
{
'FileUrl': 'https://link-to-pdf-only-available-for-24-hours',
'MbOut': 0.08830547332763672,
'Seconds': 9.43,
'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.Chrome.html_to_pdf('<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 ----
{'ResponseId': '5ef656a7-2856-43f4-aae3-7b580b0e0421', 'MbOut': 0.012622, 'Cost': 0.000257195288, 'Seconds': 1.208, 'Error': None, 'Success': True, 'FileUrl': 'https://storage.googleapis.com/a2p-v2-storage/5ef656a7-2856-43f4-aae3-7b580b0e0421'}
wkhtmltopdf
Convert HTML to PDF
api_response = a2p_client.WkHtml.html_to_pdf('<p>Hello, World</p>')
Convert HTML to PDF (ldownload PDF as a file and specify a file name)
api_response = a2p_client.WkHtml.html_to_pdf('<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.WkHtml.html_to_pdf('<p>Hello, World</p>', **options)
Convert URL to PDF
api_response = a2p_client.WkHtml.url_to_pdf('http://www.api2pdf.com')
Convert URL to PDF (download PDF as a file and specify a file name)
api_response = a2p_client.WkHtml.url_to_pdf('http://www.api2pdf.com', inline=False, 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.WkHtml.url_to_pdf('http://www.api2pdf.com', **options)
Headless Chrome
Convert HTML to PDF
api_response = a2p_client.Chrome.html_to_pdf('<p>Hello, World</p>')
Convert HTML to PDF (download PDF as a file and specify a file name)
api_response = a2p_client.Chrome.html_to_pdf('<p>Hello, World</p>', inline=False, 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.Chrome.html_to_pdf('<p>Hello, World</p>', **options)
Convert URL to PDF
api_response = a2p_client.Chrome.url_to_pdf('http://www.api2pdf.com')
Convert URL to PDF (download PDF as a file and specify a file name)
api_response = a2p_client.Chrome.url_to_pdf('http://www.api2pdf.com', inline=False, 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.Chrome.url_to_pdf('http://www.api2pdf.com', **options)
Convert HTML to Image
api_response = a2p_client.Chrome.html_to_image('<p>Hello, World</p>')
Convert URL to Image
api_response = a2p_client.Chrome.url_to_image('http://www.api2pdf.com')
LibreOffice
Convert any office file to PDF, image file to PDF, email file to PDF, HTML to Word, HTML to Excel, and PDF to HTML. Any file that can be reasonably opened by LibreOffice should be convertible. Additionally, we have an endpoint for generating a thumbnail of the first page of your PDF or Office Document. This is great for generating an image preview of your files to users.
You must provide a url to the file. Our engine will consume the file at that URL and convert it.
Convert Microsoft Office Document or Image to PDF
api_response = a2p_client.LibreOffice.any_to_pdf('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx')
Convert Microsoft Office Document or Image to PDF (download PDF as a file and specify a file name)
api_response = a2p_client.LibreOffice.any_to_pdf('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx', inline=False, file_name='test.pdf')
Thumbnail or Image Preview of a PDF or Office Document or Email file
api_response = a2p_client.LibreOffice.thumbnail('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx')
Convert HTML to Microsoft Word or Docx
api_response = a2p_client.LibreOffice.html_to_docx('http://www.api2pdf.com/wp-content/uploads/2021/01/sampleHtml.html')
Convert HTML to Microsoft Excel or Xlsx
api_response = a2p_client.LibreOffice.html_to_xlsx('http://www.api2pdf.com/wp-content/uploads/2021/01/sampleTables.html')
Convert PDF to HTML
api_response = a2p_client.LibreOffice.pdf_to_html('http://www.api2pdf.com/wp-content/uploads/2021/01/1a082b03-2bd6-4703-989d-0443a88e3b0f-4.pdf')
PdfSharp - Merge / Concatenate Two or More PDFs, Add bookmarks to pdfs, add passwords to 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.PdfSharp.merge(links_to_pdfs)
Merge PDFs from list of URLs to existing PDFs (download PDF as a file and specify a file name)
links_to_pdfs = ['https://LINK-TO-PDF', 'https://LINK-TO-PDF']
merge_result = a2p_client.PdfSharp.merge(links_to_pdfs, inline=True, file_name='test.pdf')
Add bookmarks to existing PDF
url = 'https://link-to-pdf'
bookmarks = [
{ 'Page': 0, 'Title': 'Introduction' },
{ 'Page': 1, 'Title': 'Second page' }
]
response = a2p.PdfSharp.add_bookmarks(url, bookmarks)
Add password to existing PDF
url = 'https://link-to-pdf'
password = 'hello'
response = a2p.PdfSharp.add_password(url, password)
Extract pages, or split an existing PDF
url = 'https://link-to-pdf'
start = 0
end = 2
response = a2p.PdfSharp.extract_pages(url, start, end)
Helper Methods
Api2PdfResponse: download_file()
On any Api2PdfResponse
that succesfully generated a pdf, you can use the handy download_file()
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.PdfSharp.merge(links_to_pdfs)
pdf_as_file_object = merge_result.download_file()
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.Chrome.html_to_pdf('<p>Hello World</p>')
response_id = api_response.result['ResponseId']
# delete the pdf
a2p_client.delete(response_id)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file api2pdf-2.1.0.tar.gz
.
File metadata
- Download URL: api2pdf-2.1.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac9184c8a7a0ba455a5c2f6bc44cda5deb4363d671ef95e20bb91a4abfacb18b |
|
MD5 | 65b3534b057e67b7f3a507d949e770e1 |
|
BLAKE2b-256 | 68d173ccf8d9fc119998f7d741575f12987fe1c20337387da95f0ddf7c517c5e |
File details
Details for the file api2pdf-2.1.0-py3-none-any.whl
.
File metadata
- Download URL: api2pdf-2.1.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1f86ab4adbc1c83f4945cd204ef0c3282b963241c8a56fcd61ad4a84e5ddcbc |
|
MD5 | 833df0c35debb751ebfaef7f5cd82e6b |
|
BLAKE2b-256 | f7b969c76ee0258044cb80e68965f0c52c64e82b3b03ba7c1296660f1e9bdb51 |