Download page as pdf in python
by specter - Friday June 16, 2023 at 04:50 AM
#1
import argparse
import pyppdf
import re
from pyppeteer.errors import PageError, TimeoutError, NetworkError


def main():
    parser = argparse.ArgumentParser(description = 'Page Downloader as PDF')
    parser.add_argument('--link', '-l', action = 'store', dest = 'link',
                        required = True, help = 'Inform the link to download.')
    parser.add_argument('--name', '-n', action = 'store', dest = 'name',
                        required = False, help = 'Inform the name to save.')

    arguments = parser.parse_args()

    url = arguments.link

    if not arguments.name:
        name = re.sub(r'^\w+://', '', url.lower())
        name = name.replace('/', '-')
    else:
        name = arguments.name

    if not name.endswith('.pdf'):
        name = name + '.pdf'

    print(f'Name of the file: {name}')

    try:
        pyppdf.save_pdf(name, url)
    except PageError:
        print('URL could not be resolved.')
    except TimeoutError:
        print('Timeout.')
    except NetworkError:
        print('No access to the network.')

if __name__ == '__main__':
    main()
Reply
#2
thank u for sharing this
Reply
#3
Cool , Thanks
Reply
#4
wooh  Idea
Reply
#5
Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Guide Basic way to clone any page copernicus 4 1,371 10-18-2026, 04:15 AM
Last Post: piroca123123
  Python pathlib: suffix, suffixes and stem for compound filenames nolookshot 0 191 10-16-2026, 02:41 PM
Last Post: nolookshot
  Python timezone-aware timestamps versus elapsed durations nolookshot 0 104 10-16-2026, 02:40 PM
Last Post: nolookshot
  Python CSV: handling quoted commas and embedded newlines nolookshot 0 112 10-16-2026, 02:39 PM
Last Post: nolookshot
  DOWNLOAD ALMOST ANY EDUCATIONAL BOOK FOR FREE ⭐ DON'T PAY FOR E-BOOKS MINDHUNTER 42 6,042 10-01-2026, 10:58 PM
Last Post: dddgu



 Users browsing this thread: