Enter your email id to get regular update free
Login
Your Email
Pasword

How to convert HTML to PDF Using DocRaptor?

In Python DocRaptor allows you to convert HTML document to PDF.


Open the given link and install the DocRaptor:
 https://docraptor.com/documentation/python
For installation use pip type :
pip install docraptor:
To create PDF from HTML document is quite simple you have to post your HTML to DocRaptor and you will get the response as text or plain.
import docraptor
docraptor.configuration.username = "YOUR_KEY_HERE"
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)
The above code will create a pdf file named output.pdf .
If you want to generate PDFs asynchronously:
import docraptor
import time
docraptor.configuration.username = "API_KEY" 
doc_api = docraptor.DocApi()
 try:
 create_response = doc_api.create_async_doc
({ "test": True,
"document_url": "https://docraptor.com/documentation/api",
 "name": "docraptor-python.pdf",
"document_type": "pdf", 
"javascript":  
})
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)
HTML
PDF
Converttion
DocRaptor
Ayesha
Tech writer at newsandstory
Total 107 views