1. 程式人生 > >Convert HTML to PDF Like a Boss Using DocRaptor

Convert HTML to PDF Like a Boss Using DocRaptor

(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!

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:

  1. Support any size document, with simple, per-document pricing.
  2. Supports layout and sizing changes on a per-page basis
  3. Asynchronous document generation, for long or large documents.
  4. 99.99% uptime guarantee

Installing DocRaptor

To intall DocRaptor using pip type the following command:

1pip install docraptor

You can also use easy_intall:

1easy_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.

12345678910111213141516importdocraptordocraptor.configuration.username="YOUR_API_KEY_HERE"# docraptor.configuration.debug = Truedoc_api=docraptor.DocApi()response=doc_api.create_doc({"test":True,"document_url":"https://docraptor.com/documentation/api","name":"docraptor-python.pdf","document_type":"pdf",})withopen('output.pdf',"wb")asf: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:

12345678910111213141516171819202122232425262728293031323334353637383940414243importdocraptorimporttimedocraptor.configuration.username="YOUR_API_KEY_HERE"# this key works for test documents# docraptor.configuration.debug = Truedoc_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# },})whileTrue:status_response=doc_api.get_async_doc_status(create_response.status_id)ifstatus_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.closeprint("Wrote PDF docraptor-python.pdf to current working directory")breakelifstatus_response.status=="failed":print("FAILED")print(status_response)breakelse:time.sleep(1)exceptdocraptor.rest.ApiException aserror: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!