Skip to content

Commit

Permalink
Download page as pdf (hastagAB#196)
Browse files Browse the repository at this point in the history
* Download page as PDF.

* Contributor name.

* Pudim page typo.
  • Loading branch information
j3r3mias authored Oct 24, 2020
1 parent 21b89e1 commit 957f7ab
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Download-page-as-pdf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Download Page as PDF:

Download a page as a PDF .

#### Required Modules :
- pyppdf
```bash
pip3 install pyppdf
```
- pyppyteer
```bash
pip3 install pyppeteer
```

#### Examples of use :
- Download a page:
```bash
python download-page-as-pdf.py -l 'www.pudim.com.br'
```

- Download a page and give a pdf name:
```bash
python download-page-as-pdf.py -l 'http://www.pudim.com.br' -n 'pudim.pdf'
```
42 changes: 42 additions & 0 deletions Download-page-as-pdf/download-page-as-pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/python
# -*- coding: UTF-8 -*-

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()
2 changes: 2 additions & 0 deletions Download-page-as-pdf/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pyppdf==0.1.2
pyppeteer==0.2.2
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ So far, the following projects have been integrated to this repo:
|[IMDBQuerier](IMDBQuerier)|[Burak Bekci](https://github.com/Bekci)
|[URL shortener](url_shortener)|[Sam Ebison](https://github.com/ebsa491)
|[2048](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/2048)|[Krunal](https://github.com/gitkp11)
|[Download Page as PDF](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Download-page-as-pdf)|[Jeremias Gomes](https://github.com/j3r3mias)


## How to use :
Expand Down

0 comments on commit 957f7ab

Please sign in to comment.