1

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 Answers2

1

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)
Community
  • 1
  • 1
Astik Anand
  • 12,757
  • 9
  • 41
  • 51
  • Yes, for further processing generated PDF – Arsene Bwasisi Feb 03 '18 at 07:53
  • 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. – Astik Anand Feb 03 '18 at 07:56
  • Why using the method "with open" to load file will cause missing '\n' in pdf file created? – sam Feb 03 '18 at 08:00
  • I don't think it should coz it is still file object. – Astik Anand Feb 03 '18 at 08:07
  • 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. – Arsene Bwasisi Feb 05 '18 at 00:10
0

You can check pdfme library. It's the most powerful library in python to create PDF documents, and it does everything for you, so you can focus on the contents of the document.

Check the docs here.

Felipe Sierra
  • 143
  • 2
  • 12