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:

You can also use easy_intall:

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.

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:


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!


2 thoughts on “Convert HTML to PDF Like a Boss Using DocRaptor

  1. jesse bautista

    There is an easier way to use Phyton to convert websites to PDF, with less than 20 lines of code. Utilizing pdfmyurl.com, you can use this.

    import urllib2

    data = {
    ‘license’: ‘yourlicensekey’,
    ‘url’: ‘http://www.example.com’
    }
    requesturl = ‘https://pdfmyurl.com/api?license={license}&url={url}’.format(**data)
    result = urllib2.urlopen(requesturl)
    localFile = open(‘mypdf.pdf’, ‘w’)
    localFile.write(result.read())
    localFile.close()

    Reply
  2. jesse bautista

    These are quite long commands for beginners. You might want to check out the following API providers PDFmyURL and HTM2PDF, which both offer a very complete API to convert web pages to PDF including full CSS2 and JavaScript, but also many layout options and options to apply a watermark or password to the resulting PDF.

    PDFmyURL has a HTML to PDF API as well as a full blown SDK that saves you a lot of programming. Definitely a time saver. Also, PDFmyURL offers a very extensive web to PDF API with a lot of options. Their pricing is also very transparent as opposed to many other providers. They charge in number of PDFs/month instead of credits that nobody understands.

    You can check their Python tutorial here: http://pdfmyurl.com/html-to-pdf-api-examples

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *