├── README.md └── problem-cutter.py /README.md: -------------------------------------------------------------------------------- 1 | # codeforces-problems-to-pdf 2 | ## Requirements 3 | - pdfkit 4 | - PyPDF2 5 | ## Install Requirements 6 | ```shell 7 | pip3 install pdfkit PyPDF2 8 | sudo apt-get install wkhtmltopdf 9 | ``` 10 | ## How to use 11 | First parameter: *problem link*
12 | Second parameter: *pdf name* **(don't miss the ".pdf")**
13 | Example:
14 | ```shell 15 | python3 problem-cutter.py https://codeforces.com/contest/96/problem/a 1.pdf 16 | ``` 17 | ## ScreenShots 18 | Before 19 | After 20 | 21 | -------------------------------------------------------------------------------- /problem-cutter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | import pdfkit 3 | import PyPDF2 4 | import sys 5 | 6 | def cut_problem_pdf(pdf_name, out_name): 7 | with open(pdf_name, "rb") as in_f: 8 | input1 = PyPDF2.PdfFileReader(in_f) 9 | output = PyPDF2.PdfFileWriter() 10 | 11 | num_pages = input1.getNumPages() 12 | 13 | for i in range(num_pages): 14 | page = input1.getPage(i) 15 | page.cropBox.lowerLeft = (10, 0) 16 | if i == 0: # if it's the first page cut the navigation bar 17 | page.cropBox.upperRight = (403, 690) 18 | else: 19 | page.cropBox.upperRight = (403, page.mediaBox.getUpperRight_y()) 20 | output.addPage(page) 21 | 22 | with open(out_name, "wb") as out_f: 23 | output.write(out_f) 24 | 25 | 26 | def download_problem_pdf(url, name): 27 | options = { 28 | 'margin-top': '3px', 29 | 'margin-right': '8px', 30 | 'margin-left': '8px', 31 | 'margin-bottom': '3px', 32 | } 33 | pdfkit.from_url(url, name, options=options) 34 | 35 | 36 | if __name__ == "__main__": 37 | if len(sys.argv) < 3: 38 | print("Please provide the problem link and the pdf name") 39 | exit(1) 40 | link = sys.argv[1] 41 | out_name = sys.argv[2] 42 | pdf_name = "original_problem.pdf" 43 | download_problem_pdf(link, pdf_name) 44 | cut_problem_pdf(pdf_name, out_name) 45 | --------------------------------------------------------------------------------