1
1
# 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 )
3
3
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** .
5
5
6
6
- [ Installation] ( #installation )
7
7
- [ Resources] ( #resources )
@@ -44,26 +44,26 @@ All usage starts by calling the import command and initializing the client by pa
44
44
45
45
Once you initialize the client, you can make calls like so:
46
46
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>')
48
48
print(api_response.result)
49
49
50
50
### Result Object
51
51
52
52
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.
53
53
54
54
{
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'
62
62
}
63
63
64
64
For debugging, you can print the ` Api2PdfResponse ` object to see the request and response data.
65
65
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>')
67
67
print(api_response)
68
68
69
69
Output:
@@ -74,18 +74,18 @@ Output:
74
74
- Payload:
75
75
{'html': '<p>Hello, World</p>'}
76
76
---- 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 '}
78
78
79
79
80
80
### <a name =" wkhtmltopdf " ></a > wkhtmltopdf
81
81
82
82
** Convert HTML to PDF**
83
83
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>')
85
85
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)**
87
87
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')
89
89
90
90
** Convert HTML to PDF (use keyword arguments for advanced wkhtmltopdf settings)**
91
91
[ View full list of wkhtmltopdf options available.] ( https://www.api2pdf.com/documentation/advanced-options-wkhtmltopdf/ )
@@ -94,15 +94,15 @@ Output:
94
94
'orientation': 'landscape',
95
95
'pageSize': 'A4'
96
96
}
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)
98
98
99
99
** Convert URL to PDF**
100
100
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')
102
102
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)**
104
104
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')
106
106
107
107
** Convert URL to PDF (use keyword arguments for advanced wkhtmltopdf settings)**
108
108
[ View full list of wkhtmltopdf options available.] ( https://www.api2pdf.com/documentation/advanced-options-wkhtmltopdf/ )
@@ -111,7 +111,7 @@ Output:
111
111
'orientation': 'landscape',
112
112
'pageSize': 'A4'
113
113
}
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)
115
115
116
116
117
117
---
@@ -120,70 +120,105 @@ Output:
120
120
121
121
** Convert HTML to PDF**
122
122
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>')
124
124
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)**
126
126
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')
128
128
129
129
** Convert HTML to PDF (use keyword arguments for advanced Headless Chrome settings)**
130
130
[ View full list of Headless Chrome options available.] ( https://www.api2pdf.com/documentation/advanced-options-headless-chrome/ )
131
131
132
132
options = {
133
133
'landscape': True
134
134
}
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)
136
136
137
137
** Convert URL to PDF**
138
138
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')
140
140
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)**
142
142
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')
144
144
145
145
** Convert URL to PDF (use keyword arguments for advanced Headless Chrome settings)**
146
146
[ View full list of Headless Chrome options available.] ( https://www.api2pdf.com/documentation/advanced-options-headless-chrome/ )
147
147
148
148
options = {
149
149
'landscape': True
150
150
}
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)
152
152
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')
153
160
---
154
161
155
162
## <a name =" libreoffice " ></a >LibreOffice
156
163
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.
158
165
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.
162
167
163
168
** Convert Microsoft Office Document or Image to PDF**
164
169
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')
166
171
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')
168
183
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')
170
191
171
192
---
172
193
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
174
195
175
196
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.
176
197
177
198
** Merge PDFs from list of URLs to existing PDFs**
178
199
179
200
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)
181
202
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)**
183
204
184
205
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
+
187
222
---
188
223
189
224
## <a name =" helpers " ></a >Helper Methods
@@ -198,7 +233,7 @@ a2p_client = Api2Pdf('YOUR-API-KEY')
198
233
199
234
# merge pdfs
200
235
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)
202
237
203
238
pdf_as_file_object = merge_result.download_pdf()
204
239
```
@@ -212,8 +247,8 @@ from api2pdf import Api2Pdf
212
247
a2p_client = Api2Pdf('YOUR-API-KEY')
213
248
214
249
# 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 ']
217
252
218
253
# delete the pdf
219
254
a2p_client.delete(response_id)
0 commit comments