Printing to a PDF, SVG or Postscript file

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

Printing to a PDF, SVG or Postscript file

Post by Doctor Watson » Saturday 15th May 2021 3:22pm

Using the Printer class looks like a really hot potato. I could find some example code, but it’s always about elaborate stuff like graphics and printing them to a printer (what a surprise).
The thing I would like to do is to ‘print’ the contents of a TextArea or – better still – a file to a .PDF, .SVG or Postscript the user can use or print himself, without having to use Printer.Configure.
I tried to get Printer.Print working but can’t. Why calling it so when it can’t do what it seems to say?
So I tried Printer.Configure anyway and got stuck again. Yes, it looks really good and it does produce a PDF file. Only, it’s a completely empty one because nowhere can I find a way to tell it what content has to be printed.
One nice touch : setting MyPrinter.OutputFile = Application.path & "/Output.pdf" from the start causes Configure to choose already the option ‘Print to File’.
So how to make the two options – without (preferably) and with - using Configure work?
Again, some very simple example code for ‘beginners’ in the help browser would be a great benefit.
I hope you like the little programme
' Gambas class file

Public MyPrinter As New Printer
Public fText As New TextArea(Me) As "fText"
Public MyButton As New Button(Me) As "MyButton"
Public MyButton2 As New Button(Me) As "MyButton2"

Public Sub MyButton2_Click()
With MyPrinter
'I tried :
  .print = Application.path & "/Output.pdf" 
'but as you see, that doesn't work.
End With
End

Public Sub Form_Open()

With fText
.width = 1
.height = 100
.wrap = True
.text = "Please add some text and print it to Output.pdf."
End With

With MyButton
  .width = 1
  .height = 26
  .text = "MyPrinter.Configure"
End With

With MyButton2
  .width = 1
  .height = 26
  .text = "Trying to print"
End With

With Me
  .width = 300
  .padding = 20
  .Arrangement = Arrange.Vertical
  .height = fText.Height + MyButton.Height + MyButton2.height + (.Padding * 2)
   fText.SetFocus
End With

MyPrinter.OutputFile = Application.path & "/Output.pdf"

End

Public Sub MyButton_Click()
  MyPrinter.Configure
End
Old african saying:
You eat an elephant one small bite at a time.

User avatar
grayghost4
Posts: 109
Joined: Wednesday 05th December 2018 5:00am
Location: Marengo, illinois usa

Re: Printing to a PDF, SVG or Postscript file

Post by grayghost4 » Saturday 15th May 2021 4:39pm

this might give you an example of how to print to the printer:

https://en.wikibooks.org/wiki/Programmi ... p/Printing

User avatar
grayghost4
Posts: 109
Joined: Wednesday 05th December 2018 5:00am
Location: Marengo, illinois usa

Re: Printing to a PDF, SVG or Postscript file

Post by grayghost4 » Sunday 16th May 2021 1:14am

I have modified your program to make it print

You need to add a printer to the FMain.form ... it is located in "special"
I could not make it work without adding it to the FMain ... someone else may be able to suggest how to do it in code
' Gambas class file

 '' You need to add Printer1 to the FMain.form  
' Public Printer1 As New Printer   I could not make it work here 

Public fText As New TextArea(Me) As "fText"
Public MyButton As New Button(Me) As "MyButton"
Public MyButton2 As New Button(Me) As "MyButton2"
 

Public Sub Form_Open()
 
With fText
.width = 1
.height = 100
.wrap = True
.text = "Please add some text and print it to Output.pdf."
End With
 
With MyButton
  .width = 1
  .height = 26
  .text = "Printer1.Configure"
End With
 
With MyButton2
  .width = 1
  .height = 26
  .text = "Trying to print"
End With
 
With Me
  .width = 300
  .padding = 20
  .Arrangement = Arrange.Vertical
  .height = fText.Height + MyButton.Height + MyButton2.height + (.Padding * 2)
   fText.SetFocus
End With
 
 
End

Public Sub MyButton2_Click()
Print Application.Path
 Printer1.OutputFile = Application.path & "/Output.pdf" 
 Printer1.Configure()
  Print Printer1.Print()
  End 
  
  Public Sub Printer1_Draw()
  
    Paint.DrawText(fText.Text, 10, 10)
 
  End
 
 
Public Sub MyButton_Click()
  Printer1.Configure
  Print Printer1.Print()
    
  
End


User avatar
grayghost4
Posts: 109
Joined: Wednesday 05th December 2018 5:00am
Location: Marengo, illinois usa

Re: Printing to a PDF, SVG or Postscript file

Post by grayghost4 » Sunday 16th May 2021 2:14am

this thread may help you get a handle on printing to the printer :

https://forum.gambas.one/viewtopic.php?t=813

Doctor Watson
Posts: 84
Joined: Wednesday 22nd August 2018 7:55am

Re: Printing to a PDF, SVG or Postscript file

Post by Doctor Watson » Sunday 16th May 2021 5:20am

Now we are talking Grayghost4. Your added code works.
First : you can add the printer like this :
 Public Printer1 As New Printer As "Printer1"
No need for the usual (Me) as in NewPrinter(Me). Why? Don’t know but it works. I made the same mistake.
But now for something else. The link you included, I did visit that page before.
Although they start by saying that “printing to a file will save paper”, the first example is about printing to a printer. As I did want to create a PDF, I paid no more attention to it then but I did now because I need to learn about the printing business.
Started reading and got to the example “Print a Class List”. And Lo! there it was :
Sub bPrint_Click()
  Prn.OutputFile = User.Home &/ "Names.pdf" 'I'm printing to a pdf file
  If Prn.Configure() Then Return
  GetNames
  Prn.Print()
End
That must be it! Downloaded the example (too long to insert it here).
It runs, but ….. the result is a Blank Empty PDF Page.
Did I do something wrong? So I downloaded the first example. The VERY simple one. Lots of explaining how and why. Very promising. Here we go again.
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"

End

Public Sub pr1_Draw()
  Paint.Font = Font["Linux Libertine,12"]
  Paint.DrawRichText(SimpleText, 960, 960, Paint.Width - 2 * 960)
End

Public Sub bPrintPlain_Click()
  If pr1.Configure() Then Return 'if user clicks Cancel, don't continue
  pr1.Print
End
And the result is A Wasted Completely Blanc Page
What kind of a site is this ‘Programming Gambas from Zip’? Who wrote this stuff?
What did I learn yesterday? ZIP indeed.
Today things start looking better.
Old african saying:
You eat an elephant one small bite at a time.

User avatar
grayghost4
Posts: 109
Joined: Wednesday 05th December 2018 5:00am
Location: Marengo, illinois usa

Re: Printing to a PDF, SVG or Postscript file

Post by grayghost4 » Sunday 16th May 2021 2:07pm

I have found that printing to a printer or to a pdf file or preview view is different when using qt4 or qt5 as opposed to using GTK 3 ...... kinda trial and error.

the listing from gambas from zip will work with qt4 as is, or it will work with GTK3 by changing the line:
Paint.DrawRichText(SimpleText, 960, 960, Paint.Width - 2 * 960)
E
To:
Paint.DrawRichText(SimpleText, 10,10)
It depends on the printer driver with your linux and which graphic system you use ... and what printer you have, as to how many dots per inch you have to use.
some use 72 per inch some 96 and some use 300
on my setup to get the same results in a preview and on the printer using : gb.gui , I need to use the setting :

  Printer1.Resolution = 64
   Paint.FontScale = 1
I have to use both commands together to get the proper results, to get the same dpi on the printer and the preview
I do wish there was a standard

Doctor Watson
Posts: 84
Joined: Wednesday 22nd August 2018 7:55am

Re: Printing to a PDF, SVG or Postscript file

Post by Doctor Watson » Monday 17th May 2021 6:45am

Grayghost4, it doesn’t stop there. Meanwhile I spotted the culprit in their code.
But – and don’t get me wrong – I’m only interested in obtaining a PDF file, and that’s because of those eternal problems with printers. I do appreciate your effords.
Back to the issue. The syntax of this ‘command’ should be
Paint.DrawRichText(SimpleText, X, Y, Width, Height, Alignment)
When you only indicate the ‘upper left corner’ – X & Y – Paint seems to be quite happy. So your solution with X = 10 & Y = 10 works.
But what with those values of both X and Y set to 960 ? And next : Height set to "Paint.Width - 2 * 960" ?
I found out that with - how should I put it - ‘my configuration’, Paint.Width equals 559 and Height 783.
Add a button on the form and run
Button1.text = Str$(Int(Paint.width)) & " - " & Str$(Int(Paint.height))
in Sub pr1_Draw()
As I see it, they set the ‘upper left corner’ way outside the page. And what about a Height of -1361 (minus 1361)? And with such a line of code the author(s) would like to explain to us how to start printing?
Next point : Now that I finally got the PDF showing what it should, I wanted to change the font as I don’t see anywhere what's the default font.
I found (I think) this has to be set at the start of the drawing procedure, so I added one.
Public Sub pr1_Draw()
  Paint.Font = Font["Ubuntu,12"] 'I looked it up. The Ubuntu font is on my system
  Paint.DrawRichText(SimpleText, 10, 10)
End
Result : You guessed it. ZIP (or better : RIP). It’s a blanc empty PDF once again. Tried other fonts, different size as well.
This turns out not to be a hot potato but an extremely scalding one.
Conclusion : I would like to find out once and for all if it is at all possible, using the Gambas Print, Paint, Whatever Class to produce a PDF file that people can use without further problems. If not, I might as well throw in the towel.
I’m going to put this in a new topic.
Old african saying:
You eat an elephant one small bite at a time.

User avatar
grayghost4
Posts: 109
Joined: Wednesday 05th December 2018 5:00am
Location: Marengo, illinois usa

Re: Printing to a PDF, SVG or Postscript file

Post by grayghost4 » Monday 17th May 2021 1:53pm

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 the 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.

Doctor Watson
Posts: 84
Joined: Wednesday 22nd August 2018 7:55am

Re: Printing to a PDF, SVG or Postscript file

Post by Doctor Watson » Tuesday 18th May 2021 3:20am

Grayghost4 :
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.
If that were the case, then the authors should have done so themselves in the first place, shouldn’t they?
Old african saying:
You eat an elephant one small bite at a time.

Post Reply