Advertisement
AyanUpadhaya

Web Page To Pdf Coverter in Python using API

Jun 7th, 2021
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. # Web page to pdf converter
  2. # local web page to pdf converter
  3. # python application using pdfcrowd module: 'pip install pdfcrowd'
  4. # API-application programming interface
  5.  
  6. """IMPORTANT THING - SAVE AS .pdf file"""
  7.  
  8. import pdfcrowd
  9. import sys
  10. import os
  11.  
  12. API_NAME= 'demo'
  13. API_KEY= "ce544b6ea52a5621fb9d55f8b542d14d"
  14.  
  15. #convert web to pdf
  16. def convertWebToPdf(url,filename):
  17.     #filename = converted file name, extension as .pdf
  18.     try:
  19.         api_client = pdfcrowd.HtmlToPdfClient(API_NAME, API_KEY)
  20.         api_client.convertUrlToFile(url, filename)
  21.  
  22.     except pdfcrowd.Error as why:
  23.         #report the Error
  24.  
  25.         sys.stderr.write("PdfCrowd Error: {}\n".format(why))
  26.  
  27. #covert local html file to pdf
  28. def convertHTMLtoPdf(localHTML,filename):
  29.     #localHTML = local html web page path
  30.     #filename = converted file name, extension as .pdf
  31.     try:
  32.         api_client = pdfcrowd.HtmlToPdfClient(API_NAME, API_KEY)
  33.         api_client.convertFileToFile(localHTML, filename)
  34.  
  35.     except pdfcrowd.Error as why:
  36.         #report the Error
  37.         sys.stderr.write("PdfCrowd Error: {}\n".format(why))
  38.  
  39. run = True
  40. while run:
  41.     print("Welcom to PdfCrowd Converter")
  42.     print("=============================")
  43.     print("Press i to convert web to pdf")
  44.     print("Press h to convert local html page to pdf")
  45.     print("Press q to quit")
  46.     option=input('>')
  47.  
  48.     if option == "i":
  49.         url_address=input("Enter url:")
  50.         file_name=input("Save as:")
  51.         if 'pdf' in file_name:
  52.             convertWebToPdf(url_address,file_name)
  53.             print("Success !")
  54.             print()
  55.         else:
  56.             print("wrong extension")
  57.             continue
  58.     elif option == "h":
  59.         localfile=input("Path of Html:")
  60.         file_name=input("Save as:")
  61.         if 'pdf' in file_name:
  62.             convertHTMLtoPdf(localfile,file_name)
  63.             print("Success !")
  64.             print()
  65.         else:
  66.             print("wrong extension")
  67.             continue
  68.  
  69.     elif option=='q':
  70.         run=False
  71.  
  72.     else:
  73.         print("wrong input")
  74.         print()
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement