(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
Convert HTML to PDF Like a Boss Using DocRaptor
Updated on Jan 07, 2020
DocRaptor is an awesome package which allows you to convert your HTML document to PDF. It works with Python 2 and Python 3.
Here are some features of DocRaptor:
- Support any size document, with simple, per-document pricing.
- Supports layout and sizing changes on a per-page basis
- Asynchronous document generation, for long or large documents.
- 99.99% uptime guarantee
Installing DocRaptor #
To intall DocRaptor using pip type the following command:
pip install docraptor
You can also use easy_intall
:
easy_intall install docraptor
Creating PDFs #
Creating PDF from you HTML docment is quite simple just post your HTML to DocRaptor and you will get the response as text/plain
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import docraptor
docraptor.configuration.username = "YOUR_API_KEY_HERE"
# docraptor.configuration.debug = True
doc_api = docraptor.DocApi()
response = doc_api.create_doc({
"test": True,
"document_url": "https://docraptor.com/documentation/api",
"name": "docraptor-python.pdf",
"document_type": "pdf",
})
with open('output.pdf', "wb") as f:
f.write(response)
|
Run the script and it will create a pdf file named output.pdf
.
The preceding code generates PDF synchronously. Here is how you can generate PDFs asynchronously:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import docraptor
import time
docraptor.configuration.username = "YOUR_API_KEY_HERE" # this key works for test documents
# docraptor.configuration.debug = True
doc_api = docraptor.DocApi()
try:
create_response = doc_api.create_async_doc({
"test": True, # test documents are free but watermarked
# "document_content": "Hello World", # supply content directly
"document_url": "https://docraptor.com/documentation/api", # or use a url
"name": "docraptor-python.pdf", # help you find a document later
"document_type": "pdf", # pdf or xls or xlsx
"javascript": True, # enable JavaScript processing
# "prince_options": {
# "media": "screen", # use screen styles instead of print styles
# "baseurl": "http://hello.com", # pretend URL when using document_content
# },
})
while True:
status_response = doc_api.get_async_doc_status(create_response.status_id)
if status_response.status == "completed":
doc_response = doc_api.get_async_doc(status_response.download_id)
file = open("./docraptor-python.pdf", "wb")
file.write(doc_response)
file.close
print("Wrote PDF docraptor-python.pdf to current working directory")
break
elif status_response.status == "failed":
print("FAILED")
print(status_response)
break
else:
time.sleep(1)
except docraptor.rest.ApiException as error:
print(error)
print(error.message)
print(error.code)
print(error.response_body)
|
Other Tutorials (Sponsors)
This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!
View Comments