I'm a beginner programmer, tryna teach myself to code. One of the ways I do that is I give myself projects to work on. Right now I'm working on a PDF generator application and my question is where can I start in build this application? I've heard of SDKs and APIs, but I'm not really sure how to use them in my code, whether I have to import a file for it, or something of that sort. Any advice would be much appreciated.
2 Answers
Try using python-pdfkit
Installation
pip install pdfkit
PDF Generation
import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')
pdfkit.from_file('test.html', 'out.pdf')
pdfkit.from_string('Hello!', 'out.pdf')
You can pass a list with multiple URLs or files:
pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf')
pdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')
Also you can pass an opened file:
with open('file.html') as f:
pdfkit.from_file(f, 'out.pdf')
If you wish to further process generated PDF, you can read it to a variable:
# Use False instead of output path to save pdf to a variable
pdf = pdfkit.from_url('http://google.com', False)
5 Comments
Arsene Bwasisi
Yes, for further processing generated PDF
Astik Anand
Yeah, I mean you have created the pdf and stored it in a variable, and further you can highlight some words or do some other things after storing it in some variable. That variable will help to do all these things.
sam
Why using the method "with open" to load file will cause missing '\n' in pdf file created?
Astik Anand
I don't think it should coz it is still file object.
Arsene Bwasisi
I'm tryna figure out how I can input any file into the program so it can be converted into PDF. I tried the input function in order for me to input the file in the terminal and have the program open and read it, but with no luck. I keep getting errors. How can I input the document in the first place so it can be converted. And how can I do this so that I can input any document without having to rewrite my code.