PDF quandary - A cry for clarity.

Post your Gambas programming questions here.
Doctor Watson
Posts: 84
Joined: Wednesday 22nd August 2018 7:55am

PDF quandary - A cry for clarity.

Post by Doctor Watson »

Those who follow my “Printing to a PDF, SVG or Postscript file” topic will know that trying to do so seems to be a real problem.
It doesn’t make sense reading through pages of instructions and code examples, trying to learn how to make project work, only to find out that when it finally does it will only run on a machine with the same or at least compatible hard and software features as the one you’re using. When you can’t make sure that people will be able to obtain a proper result by using the executable you want to distribute, what’s the point?
Nowhere is this seemingly more apparent than in the complex procedures related to printing.
Mind you, this is not a typical Gambas issue. I’ve encountered it in other programming languages as well.
That’s why I want to put the output-to-be-printed in a PDF file. That one (or SVG, or Postscript) can be used on any system using the appropriate software. Users running Linux may want to send their file to Windows, Mac, ...
So I would like to find out once and for all if it is at all possible, using the Gambas Print, Paint or Whatever Class to produce a PDF file that people can use without further problems. If not, I might as well throw in the towel.
That’s why I would like to ask you to run the following code – and eventually modify it - and see if you can obtain a ‘working’ PDF file. If you don’t want to save the file, just opt for ‘Print Preview’.
I’m curious. If that already turns out to be a problem ... :cry:
The original project can be found here (oh dear) :
https://en.wikibooks.org/wiki/Programmi ... p/Printing
' Gambas class file

Public pr1 As New Printer As "pr1"
Public Button1 As New Button(Me) As "Button1"
Public SimpleText As String

Public Sub Form_Open()
  
SimpleText = "Countries of the World<br><br>Papua New Guinea<br><br>Papua New Guinea is a country that is north of Australia. It has much green rainforest. It has beautiful blue seas. Its capital, located along its southeastern coast, is Port Moresby.<br><br>This is plain text in Linux Libertine 12 point.<br><br>John Smith, editor"

Me.width = 140
Me.Height = 100
With Button1
  .width = 100
  .height = 30
  .left = 20
  .top = 20
  .text = "Click"
End With
End

Public Sub pr1_Draw()
  Paint.Font = Font["Linux Libertine,12"]  'Turn this line Off or you won't see anything
  Paint.DrawRichText(SimpleText, 10, 10)   'Original was (SimpleText,960, 960, Paint.Width - 2 * 960) what ????
End

Public Sub Button1_Click()
  pr1.OutputFile = Application.path & "/Output.pdf" 'To make sure Configure is set 'Print To File'
  If pr1.Configure() Then Return
  pr1.Print
End
Old african saying:
You eat an elephant one small bite at a time.
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: PDF quandary - A cry for clarity.

Post by BruceSteers »

Are you not using poppler?

I know not what exactly poppler does but it is the gambas pdf component, i'd should think it worth using if you want to do pdf related things.
It might just be for reading pdf docs not writing

http://gambaswiki.org/wiki/comp/gb.poppler
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: PDF quandary - A cry for clarity.

Post by BruceSteers »

All your code does is print text to a file.

Just because you name it *.pdf does not make it a pdf file. :roll:

I should think you will have to use an external program to convert the print text into pdf format

Or learn how to manually write pdf format.

It's not going to happen all by itself ;)

Welcome to the wonderfull world of linux ;) :lol:
If at first you don't succeed , try doing something differently.
BruceS
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: PDF quandary - A cry for clarity.

Post by vuott »

Hello Doctor Watson,
I have tried your code and it seems to me that the pdf file is created regularly.
In fact, opening it with a hexadecimal editor, I can see its "Magic Number ": %PDF-1.4.%.....1 et cetera...

However, another way to create a PDF file is to use gb.cairo Component.
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
Doctor Watson
Posts: 84
Joined: Wednesday 22nd August 2018 7:55am

Re: PDF quandary - A cry for clarity.

Post by Doctor Watson »

The generated files seem to be real PDF files indeed.
Just checked them with an app ‘PDF Mix Tool’ and the Document Information says (Vuott will be pleased) : “Producer cairo 1.16.0”.
So printer.configure uses cairo to produce PDF files.
How this will turn out when I try to reach my final goal – putting a GridView on a PDF page :?: :roll:
But for now that leaves the eminent problem of using fonts.
Old african saying:
You eat an elephant one small bite at a time.
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: PDF quandary - A cry for clarity.

Post by vuott »

Doctor Watson wrote: Monday 17th May 2021 12:56pm...my final goal – putting a GridView on a PDF page
In the pdf file do you want to put only the text contained in the cells of GridView, or do you also want to make the grid appear?
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: PDF quandary - A cry for clarity.

Post by grayghost4 »

on my system Debian , Gnome

first press of the print button produces a blank output to either the file or preview.

all successive pressed of the print button produce proper output to both file or preview

I have not been able to find out why.

so during testing of you only test once and stop to try to find out why .... you think it is not working ! But it may work on the second press of print .... hopefully someone else can find the problem and the fix.

This is duplicate from my post in the other thread of yours
the person that wrote the original code from the from zip book was using qt graphics and you and I are using gtk.

when you start a new project you are given the choice which to use ... it can be changed later but it is kinda confusing as to just what changes go together ... if you create a new project and chose qt for graphics I think the code from the book will work just fine.
If you ask someone to run a test of your code from a copy and paste you should tell them what graphic system you are using.

When you compile the project I think the graphic system is compiled with the code ....I have not published anything yet so I am not sure .....others can answer that.
Last edited by grayghost4 on Monday 17th May 2021 2:14pm, edited 1 time in total.
vuott
Posts: 262
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: PDF quandary - A cry for clarity.

Post by vuott »

Well... if you want to "put" your GridView and its text in a PDF file, you could try this example-code.
You have to activate "gb.desktop.x11" and "gb.cairo" Components.
Public Sub Form_Open()

  Dim r, c As Byte

  With GridView1
    .Rows.Count = 5
    .Columns.Count = 5
  End With
  
  For r = 0 To 4
    For c = 0 To 4
      GridView1[r, c].Text = "abcde"
    Next 
  Next

End

Public Sub Button1_Click()
  
  Dim dw As DesktopWindow
  Dim pdf As CairoPdfSurface
  Dim im As Image

' Find the "window" of the GridView by its identifier
  With dw = New DesktopWindow(GridView1.Handle)
' Capture the GridView image:
    im = .GetScreenshot(True).Image
  End With

' Sets the drawing surface of the future PDF file.
' The first argument sets the path where the future PDF file will be saved.
' The second and third arguments set the size in "millimeters" of the future PDF file.
  pdf = New CairoPdfSurface("/tmp/file.pdf", 210.0, 297.0)
  
  With Cairo
    .Begin(pdf)
    .Source = Cairo.ImagePattern(im, 100, 100)
    .Paint 
    .End
  End With

End
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: PDF quandary - A cry for clarity.

Post by BruceSteers »

I stand corrected :roll:

thanks guys
I should probably not comment with assumptions when i know very little about something, like printing ;)
If at first you don't succeed , try doing something differently.
BruceS
Doctor Watson
Posts: 84
Joined: Wednesday 22nd August 2018 7:55am

Re: PDF quandary - A cry for clarity.

Post by Doctor Watson »

Grayghost4, you are so right. To all who want to try it:
- Feel free to change "Linux Libertine,12" to some font you like. "Z003,20" is a nice one. It should be pre-installed.
- Run my code and click on the button.
- Printer.Configure appears.
- Choose ‘Print Preview’ (you will get a blanc page)
- Close the Preview screen, not the programme.
- Click on the button again.
- Printer.configure appears again.
- Choose ‘Print Preview’ again.
- This time the preview screen displays the text in the choosen font.
Just as Grayghost4 I haven’t the foggiest why this happens but is looks like a clue to the problem. Real Guru stuff this one.
Vuott : thanks for the code. I will have a go at it soon.
Bruce : No hard feelings mate ! :D
Old african saying:
You eat an elephant one small bite at a time.
Post Reply