Python Forum
Creating a pdf from file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Creating a pdf from file (/thread-25560.html)



Creating a pdf from file - DT2000 - Apr-03-2020

I have been working on getting my program to make a PDF of all the data in various widgets. I have been able to get the data and it does create a PDF file however the formatting is not correct. I need to learn know how to make the lines of the populated widget but the PDF made has a line of data for each widget and the widgets that have a larger amount of data is posted one a single line.

As an example, this code should look like below:
  ;======================================
  ;-Program: GOJHL v2.1
  ; Language: Pure Basic
  ; Written by: John Jr.
  ; Date: April 08, 2016
  ; Copyright 2014-2016
But it saves in the PDF as:
;====================================== ;-Program: GOJHL v2.1 ; Language: Pure Basic ; Written by: John Coones Jr. ; Date: April 08, 2016 ; Copyright 2014-2016

Here is my current PDF code:
def makePDF():
    try:
        pdf=FPDF(orientation='P', unit='mm', format='A3')
        pdf.add_page()
        pdf.set_font("Arial", size = 10)
        pdf.cell(200, 10, txt = "PBSnippet",
                 ln = 1, align = 'L')

        f = (Code_Type.get(), Snippet_Name.get(), Code_Snippet.get('1.0', END), Description.get('1.0',END), Date.get(), Author.get(), Version.get())

        for x in f:
            pdf.cell(200, 10, txt = x, ln = 1, align = 'L')

        pdf.output("PBSnippet.pdf")

    except:
        messagebox.showerror('PBSnippet', 'Failed to create the PDF!')
The data from Description and Code_Snippet are multiple lines and are displaying as a single line as shown above.
A little help to get the data to save in PDF as it shows in the widget would be appreciated.

Thanks again.


RE: Creating a pdf from file - Mateusz - Apr-03-2020

Hi, I tried this code on my computer:
from fpdf import FPDF
    
pdf = FPDF(orientation='P', unit='mm', format='A3')
pdf.add_page()
pdf.set_font("Arial", size = 10)
pdf.cell(200, 10, txt = "PBSnippet",ln = 1, align = 'L')
 
f = (";======================================", ";-Program: GOJHL v2.1", "; Language: Pure Basic", "; Written by: John Jr.", "; Date: April 08, 2016", "; Copyright 2014-2016")
for x in f:
    pdf.cell(200, 10, txt = x, ln = 1, align = 'L')
 
pdf.output("PBSnippet.pdf")
and output:
Quote:PBSnippet
;======================================
;-Program: GOJHL v2.1
; Language: Pure Basic
; Written by: John Jr.
; Date: April 08, 2016
; Copyright 2014-2016


Are you sure that you execute exactly code witn ln=1?
Which version of fpdf do you have? 1.7.2 ?


RE: Creating a pdf from file - DT2000 - Apr-03-2020

I believe it is version 1.7.2, I installed it last evening.

The problem is that I am getting the data using the ".get()" method from the widgets and not typing it into the "f =" directly.
Even if I use only 1 of the widget and make the PDF it then puts 1 letter per line instead of a single row of text for each
.get()


RE: Creating a pdf from file - Mateusz - Apr-03-2020

Quote:The data from Description and Code_Snippet are multiple lines and are displaying as a single line as shown above
Please provide example for multiple lines.

What kind of widgets they are? Tkinter? WxWidgets? I tried to mix f content with empty '', something with '\n' inside and everytime have output line after line. It's interesting :)


RE: Creating a pdf from file - DT2000 - Apr-03-2020

I am writing it with tkinter and they at Listbox.
The data is coming from 7 widgets in total.
5 Entry widgets - (Date, Author, Snippet_Name, Version, Code_Type)
2 Listbox widgets - (Code_Snippet, Description)