Created
February 18, 2019 04:03
-
-
Save apexdodge/a30c2c942369ca6ea01217f53b6b3a3f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Module for manipulating and generating PDFs""" | |
import requests | |
import json | |
from api2pdf import Api2Pdf | |
API2PDF_API_KEY = 'YOUR-API-KEY' # get key from portal.api2pdf.com/register | |
USERAGENT = { | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'} | |
def make_pdf_from_url(url, options=None): | |
"""Produces a pdf from a website's url. | |
Args: | |
url (str): A valid url | |
options (dict, optional): for specifying pdf parameters like landscape | |
mode and margins | |
Returns: | |
pdf of the website | |
""" | |
a2p_client = Api2Pdf(API2PDF_API_KEY) | |
pdf_response = a2p_client.HeadlessChrome.convert_from_url(url, options=options) | |
if pdf_response['success']: | |
download_response = requests.get(pdf_response['pdf'], headers=USERAGENT) | |
data = download_response.content | |
return data | |
else: | |
return None | |
def make_pdf_from_raw_html(html, options=None): | |
"""Produces a pdf from raw html. | |
Args: | |
html (str): Valid html | |
options (dict, optional): for specifying pdf parameters like landscape | |
mode and margins | |
Returns: | |
pdf of the supplied html | |
""" | |
a2p_client = Api2Pdf(API2PDF_API_KEY) | |
pdf_response = a2p_client.HeadlessChrome.convert_from_html(html, options=options) | |
if pdf_response['success']: | |
download_response = requests.get(pdf_response['pdf'], headers=USERAGENT) | |
data = download_response.content | |
return data | |
else: | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment