Skip to content

Commit 550a6b6

Browse files
authored
Merge pull request #8 from Api2Pdf/2.0.0
2.0.0
2 parents 6ae8702 + 76f6617 commit 550a6b6

File tree

3 files changed

+175
-109
lines changed

3 files changed

+175
-109
lines changed

README.md

+78-43
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# api2pdf.python
2-
Python bindings for [Api2Pdf REST API](https://www.api2pdf.com/documentation)
2+
Python bindings for [Api2Pdf REST API](https://www.api2pdf.com/documentation/v2)
33

4-
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**.
4+
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, and adding bookmarks to PDFs. Api2Pdf is a wrapper for popular libraries such as **wkhtmltopdf**, **Headless Chrome**, **PdfSharp**, and **LibreOffice**.
55

66
- [Installation](#installation)
77
- [Resources](#resources)
@@ -44,26 +44,26 @@ All usage starts by calling the import command and initializing the client by pa
4444

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

47-
api_response = a2p_client.HeadlessChrome.convert_from_html('<p>Hello, World</p>')
47+
api_response = a2p_client.Chrome.html_to_pdf('<p>Hello, World</p>')
4848
print(api_response.result)
4949

5050
### Result Object
5151

5252
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.
5353

5454
{
55-
'pdf': 'https://link-to-pdf-only-available-for-24-hours',
56-
'mbIn': 0.08421039581298828,
57-
'mbOut': 0.08830547332763672,
58-
'cost': 0.00017251586914062501,
59-
'success': True,
60-
'error': None,
61-
'responseId': '6e46637a-650d-46d5-af0b-3d7831baccbb'
55+
'FileUrl': 'https://link-to-pdf-only-available-for-24-hours',
56+
'MbOut': 0.08830547332763672,
57+
'Seconds': 9.43,
58+
'Cost': 0.00017251586914062501,
59+
'Success': True,
60+
'Error': None,
61+
'ResponseId': '6e46637a-650d-46d5-af0b-3d7831baccbb'
6262
}
6363

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

66-
api_response = a2p_client.HeadlessChrome.convert_from_html('<p>Hello, World</p>')
66+
api_response = a2p_client.Chrome.html_to_pdf('<p>Hello, World</p>')
6767
print(api_response)
6868

6969
Output:
@@ -74,18 +74,18 @@ Output:
7474
- Payload:
7575
{'html': '<p>Hello, World</p>'}
7676
---- API2PDF RESPONSE ----
77-
{'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'}
77+
{'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'}
7878

7979

8080
### <a name="wkhtmltopdf"></a> wkhtmltopdf
8181

8282
**Convert HTML to PDF**
8383

84-
api_response = a2p_client.WkHtmlToPdf.convert_from_html('<p>Hello, World</p>')
84+
api_response = a2p_client.WkHtml.html_to_pdf('<p>Hello, World</p>')
8585

86-
**Convert HTML to PDF (load PDF in browser window and specify a file name)**
86+
**Convert HTML to PDF (ldownload PDF as a file and specify a file name)**
8787

88-
api_response = a2p_client.WkHtmlToPdf.convert_from_html('<p>Hello, World</p>', inline_pdf=True, file_name='test.pdf')
88+
api_response = a2p_client.WkHtml.html_to_pdf('<p>Hello, World</p>', inline_pdf=True, file_name='test.pdf')
8989

9090
**Convert HTML to PDF (use keyword arguments for advanced wkhtmltopdf settings)**
9191
[View full list of wkhtmltopdf options available.](https://www.api2pdf.com/documentation/advanced-options-wkhtmltopdf/)
@@ -94,15 +94,15 @@ Output:
9494
'orientation': 'landscape',
9595
'pageSize': 'A4'
9696
}
97-
api_response = a2p_client.WkHtmlToPdf.convert_from_html('<p>Hello, World</p>', **options)
97+
api_response = a2p_client.WkHtml.html_to_pdf('<p>Hello, World</p>', **options)
9898

9999
**Convert URL to PDF**
100100

101-
api_response = a2p_client.WkHtmlToPdf.convert_from_url('http://www.api2pdf.com')
101+
api_response = a2p_client.WkHtml.url_to_pdf('http://www.api2pdf.com')
102102

103-
**Convert URL to PDF (load PDF in browser window and specify a file name)**
103+
**Convert URL to PDF (download PDF as a file and specify a file name)**
104104

105-
api_response = a2p_client.WkHtmlToPdf.convert_from_url('http://www.api2pdf.com', inline_pdf=True, file_name='test.pdf')
105+
api_response = a2p_client.WkHtml.url_to_pdf('http://www.api2pdf.com', inline=False, file_name='test.pdf')
106106

107107
**Convert URL to PDF (use keyword arguments for advanced wkhtmltopdf settings)**
108108
[View full list of wkhtmltopdf options available.](https://www.api2pdf.com/documentation/advanced-options-wkhtmltopdf/)
@@ -111,7 +111,7 @@ Output:
111111
'orientation': 'landscape',
112112
'pageSize': 'A4'
113113
}
114-
api_response = a2p_client.WkHtmlToPdf.convert_from_url('http://www.api2pdf.com', **options)
114+
api_response = a2p_client.WkHtml.url_to_pdf('http://www.api2pdf.com', **options)
115115

116116

117117
---
@@ -120,70 +120,105 @@ Output:
120120

121121
**Convert HTML to PDF**
122122

123-
api_response = a2p_client.HeadlessChrome.convert_from_html('<p>Hello, World</p>')
123+
api_response = a2p_client.Chrome.html_to_pdf('<p>Hello, World</p>')
124124

125-
**Convert HTML to PDF (load PDF in browser window and specify a file name)**
125+
**Convert HTML to PDF (download PDF as a file and specify a file name)**
126126

127-
api_response = a2p_client.HeadlessChrome.convert_from_html('<p>Hello, World</p>', inline_pdf=True, file_name='test.pdf')
127+
api_response = a2p_client.Chrome.html_to_pdf('<p>Hello, World</p>', inline=False, file_name='test.pdf')
128128

129129
**Convert HTML to PDF (use keyword arguments for advanced Headless Chrome settings)**
130130
[View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/)
131131

132132
options = {
133133
'landscape': True
134134
}
135-
api_response = a2p_client.HeadlessChrome.convert_from_html('<p>Hello, World</p>', **options)
135+
api_response = a2p_client.Chrome.html_to_pdf('<p>Hello, World</p>', **options)
136136

137137
**Convert URL to PDF**
138138

139-
api_response = a2p_client.HeadlessChrome.convert_from_url('http://www.api2pdf.com')
139+
api_response = a2p_client.Chrome.url_to_pdf('http://www.api2pdf.com')
140140

141-
**Convert URL to PDF (load PDF in browser window and specify a file name)**
141+
**Convert URL to PDF (download PDF as a file and specify a file name)**
142142

143-
api_response = a2p_client.HeadlessChrome.convert_from_url('http://www.api2pdf.com', inline_pdf=True, file_name='test.pdf')
143+
api_response = a2p_client.Chrome.url_to_pdf('http://www.api2pdf.com', inline=False, file_name='test.pdf')
144144

145145
**Convert URL to PDF (use keyword arguments for advanced Headless Chrome settings)**
146146
[View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/)
147147

148148
options = {
149149
'landscape': True
150150
}
151-
api_response = a2p_client.HeadlessChrome.convert_from_url('http://www.api2pdf.com', **options)
151+
api_response = a2p_client.Chrome.url_to_pdf('http://www.api2pdf.com', **options)
152152

153+
**Convert HTML to Image**
154+
155+
api_response = a2p_client.Chrome.html_to_image('<p>Hello, World</p>')
156+
157+
**Convert URL to Image**
158+
159+
api_response = a2p_client.Chrome.url_to_image('http://www.api2pdf.com')
153160
---
154161

155162
## <a name="libreoffice"></a>LibreOffice
156163

157-
LibreOffice supports the conversion to PDF from the following file formats:
164+
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.
158165

159-
- doc, docx, xls, xlsx, ppt, pptx, gif, jpg, png, bmp, rtf, txt, html
160-
161-
You must provide a url to the file. Our engine will consume the file at that URL and convert it to the PDF.
166+
You must provide a url to the file. Our engine will consume the file at that URL and convert it.
162167

163168
**Convert Microsoft Office Document or Image to PDF**
164169

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

167-
**Convert Microsoft Office Document or Image to PDF (load PDF in browser window and specify a file name)**
172+
**Convert Microsoft Office Document or Image to PDF (download PDF as a file and specify a file name)**
173+
174+
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')
175+
176+
**Thumbnail or Image Preview of a PDF or Office Document or Email file**
177+
178+
api_response = a2p_client.LibreOffice.thumbnail('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx')
179+
180+
**Convert HTML to Microsoft Word or Docx**
181+
182+
api_response = a2p_client.LibreOffice.html_to_docx('http://www.api2pdf.com/wp-content/uploads/2021/01/sampleHtml.html')
168183

169-
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')
184+
**Convert HTML to Microsoft Excel or Xlsx**
185+
186+
api_response = a2p_client.LibreOffice.html_to_xlsx('http://www.api2pdf.com/wp-content/uploads/2021/01/sampleTables.html')
187+
188+
**Convert PDF to HTML**
189+
190+
api_response = a2p_client.LibreOffice.pdf_to_html('http://www.api2pdf.com/wp-content/uploads/2021/01/1a082b03-2bd6-4703-989d-0443a88e3b0f-4.pdf')
170191

171192
---
172193

173-
## <a name="merge"></a>Merge / Concatenate Two or More PDFs
194+
## <a name="merge"></a>PdfSharp - Merge / Concatenate Two or More PDFs, Add bookmarks to pdfs, add passwords to pdfs
174195

175196
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.
176197

177198
**Merge PDFs from list of URLs to existing PDFs**
178199

179200
links_to_pdfs = ['https://LINK-TO-PDF', 'https://LINK-TO-PDF']
180-
merge_result = a2p_client.merge(links_to_pdfs)
201+
merge_result = a2p_client.PdfSharp.merge(links_to_pdfs)
181202

182-
**Merge PDFs from list of URLs to existing PDFs (load PDF in browser window and specify a file name)**
203+
**Merge PDFs from list of URLs to existing PDFs (download PDF as a file and specify a file name)**
183204

184205
links_to_pdfs = ['https://LINK-TO-PDF', 'https://LINK-TO-PDF']
185-
merge_result = a2p_client.merge(links_to_pdfs, inline_pdf=True, file_name='test.pdf')
186-
206+
merge_result = a2p_client.PdfSharp.merge(links_to_pdfs, inline=True, file_name='test.pdf')
207+
208+
**Add bookmarks to existing PDF**
209+
url = 'https://link-to-pdf
210+
bookmarks = [
211+
{ 'Page': 0, 'Title': 'Introduction' },
212+
{ 'Page': 1, 'Title': 'Second page' }
213+
]
214+
response = a2p.PdfSharp.add_bookmarks(url, bookmarks)
215+
216+
**Add password to existing PDF**
217+
218+
url = 'https://link-to-pdf
219+
password = 'hello'
220+
response = a2p.PdfSharp.add_password(url, password)
221+
187222
---
188223

189224
## <a name="helpers"></a>Helper Methods
@@ -198,7 +233,7 @@ a2p_client = Api2Pdf('YOUR-API-KEY')
198233
199234
# merge pdfs
200235
links_to_pdfs = ['https://LINK-TO-PDF', 'https://LINK-TO-PDF']
201-
merge_result = a2p_client.merge(links_to_pdfs)
236+
merge_result = a2p_client.PdfSharp.merge(links_to_pdfs)
202237
203238
pdf_as_file_object = merge_result.download_pdf()
204239
```
@@ -212,8 +247,8 @@ from api2pdf import Api2Pdf
212247
a2p_client = Api2Pdf('YOUR-API-KEY')
213248
214249
# generate a pdf
215-
api_response = a2p_client.HeadlessChrome.convert_from_html('<p>Hello World</p>')
216-
response_id = api_response.result['responseId']
250+
api_response = a2p_client.Chrome.html_to_pdf('<p>Hello World</p>')
251+
response_id = api_response.result['ResponseId']
217252
218253
# delete the pdf
219254
a2p_client.delete(response_id)

0 commit comments

Comments
 (0)