Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit fc70ea7

Browse files
authored
Merge pull request #244 from ShivSt/GeeksforGeeks
Script added for saving GeeksforGeeks article as pdf #229
2 parents ebd7bab + f815c8a commit fc70ea7

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# !/usr/bin/env python
2+
from selenium import webdriver
3+
from webdriver_manager.chrome import ChromeDriverManager
4+
import json
5+
import requests
6+
7+
8+
# article url
9+
# URL = "https://www.geeksforgeeks.org/what-can-i-do-with-python/"
10+
11+
12+
def get_driver():
13+
# chrome options settings
14+
chrome_options = webdriver.ChromeOptions()
15+
settings = {
16+
"recentDestinations": [
17+
{"id": "Save as PDF", "origin": "local", "account": ""}
18+
],
19+
"selectedDestinationId": "Save as PDF",
20+
"version": 2,
21+
}
22+
prefs = {
23+
"printing.print_preview_sticky_settings.appState": json.dumps(settings)
24+
}
25+
chrome_options.add_experimental_option("prefs", prefs)
26+
chrome_options.add_argument("--kiosk-printing")
27+
28+
# launch browser with predefined settings
29+
browser = webdriver.Chrome(
30+
executable_path=ChromeDriverManager().install(), options=chrome_options
31+
)
32+
return browser
33+
34+
35+
def download_article(URL):
36+
browser = get_driver()
37+
browser.get(URL)
38+
39+
# launch print and save as pdf
40+
browser.execute_script("window.print();")
41+
browser.close()
42+
43+
44+
if __name__ == "__main__":
45+
URL = input("provide article URL: ")
46+
# check if the url is valid/reachable
47+
if requests.get(URL).status_code == 200:
48+
try:
49+
download_article(URL)
50+
print("Your article is successfully downloaded")
51+
except Exception as e:
52+
print(e)
53+
else:
54+
print("Enter a valid working URL")
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Download GeeksForGeeks Articles as pdf
2+
<!--Remove the below lines and add yours -->
3+
This script take a link of GeeksForGeeks article as input and download the complete article as a pdf at default download location.
4+
5+
### Prerequisites
6+
<!--Remove the below lines and add yours -->
7+
* selenium
8+
* requests
9+
* webdriver-manager
10+
* Run `pip install -r requirements.txt` to install required external modules.
11+
12+
### How to run the script
13+
<!--Remove the below lines and add yours -->
14+
- Execute `python3 downloader.py`
15+
- Type in URL of article when prompted.
16+
17+
### Screenshot/GIF showing the sample use of the script
18+
<!--Remove the below lines and add yours -->
19+
![Screenshot of the Output](Screenshot.jpg)
20+
21+
## *Author Name*
22+
<!--Remove the below lines and add yours -->
23+
[Shiv Thakur](https://github.com/ShivSt)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
requests==2.24.0
2+
selenium==3.141.0
3+
webdriver-manager==3.2.2
45.1 KB
Loading

0 commit comments

Comments
 (0)