Printing to a PDF, SVG or Postscript file

Post

Posted
Rating:
#1 (In Topic #664)
Regular
Doctor Watson is in the usergroup ‘Regular’
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

Code (gambas)

  1. ' Gambas class file
  2.  
  3. Public MyPrinter As New Printer
  4. Public fText As New TextArea(Me) As "fText"
  5. Public MyButton As New Button(Me) As "MyButton"
  6. Public MyButton2 As New Button(Me) As "MyButton2"
  7.  
  8. Public Sub MyButton2_Click()
  9. With MyPrinter
  10. 'I tried :
  11.   .print = Application.path & "/Output.pdf"
  12. 'but as you see, that doesn't work.
  13.  
  14. Public Sub Form_Open()
  15.  
  16. With fText
  17. .width = 1
  18. .height = 100
  19. .wrap = True
  20. .text = "Please add some text and print it to Output.pdf."
  21.  
  22. With MyButton
  23.   .width = 1
  24.   .height = 26
  25.   .text = "MyPrinter.Configure"
  26.  
  27. With MyButton2
  28.   .width = 1
  29.   .height = 26
  30.   .text = "Trying to print"
  31.  
  32.   .width = 300
  33.   .padding = 20
  34.   .Arrangement = Arrange.Vertical
  35.   .height = fText.Height + MyButton.Height + MyButton2.height + (.Padding * 2)
  36.    fText.SetFocus
  37.  
  38. MyPrinter.OutputFile = Application.path & "/Output.pdf"
  39.  
  40.  
  41. Public Sub MyButton_Click()
  42.   MyPrinter.Configure

 Old african saying:
You eat an elephant one small bite at a time.
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
this might give you an example of how to print to the printer:

Programming Gambas from Zip/Printing - Wikibooks, open books for an open world
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
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

Code (gambas)

  1. ' Gambas class file
  2.  
  3.  '' You need to add Printer1 to the FMain.form  
  4. ' Public Printer1 As New Printer   I could not make it work here
  5.  
  6. Public fText As New TextArea(Me) As "fText"
  7. Public MyButton As New Button(Me) As "MyButton"
  8. Public MyButton2 As New Button(Me) As "MyButton2"
  9.  
  10.  
  11. Public Sub Form_Open()
  12.  
  13. With fText
  14. .width = 1
  15. .height = 100
  16. .wrap = True
  17. .text = "Please add some text and print it to Output.pdf."
  18.  
  19. With MyButton
  20.   .width = 1
  21.   .height = 26
  22.   .text = "Printer1.Configure"
  23.  
  24. With MyButton2
  25.   .width = 1
  26.   .height = 26
  27.   .text = "Trying to print"
  28.  
  29.   .width = 300
  30.   .padding = 20
  31.   .Arrangement = Arrange.Vertical
  32.   .height = fText.Height + MyButton.Height + MyButton2.height + (.Padding * 2)
  33.    fText.SetFocus
  34.  
  35.  
  36.  
  37. Public Sub MyButton2_Click()
  38.  Printer1.OutputFile = Application.path & "/Output.pdf"
  39.  Printer1.Configure()
  40.   Print Printer1.Print()
  41.   End
  42.  
  43.   Public Sub Printer1_Draw()
  44.  
  45.     Paint.DrawText(fText.Text, 10, 10)
  46.  
  47.   End
  48.  
  49.  
  50. Public Sub MyButton_Click()
  51.   Printer1.Configure
  52.   Print Printer1.Print()
  53.    
  54.  
  55.  
  56.  
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
this thread may help you get a handle on printing to the printer :

https://forum.gambas.one/viewtopic.php?t=813
Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
Doctor Watson is in the usergroup ‘Regular’
Now we are talking Grayghost4. Your added code works.
First : you can add the printer like this :

Code (gambas)

  1.  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 :

Code (gambas)

  1. Sub bPrint_Click()
  2.   Prn.OutputFile = User.Home &/ "Names.pdf" 'I'm printing to a pdf file
  3.   If Prn.Configure() Then Return
  4.   GetNames
  5.   Prn.Print()
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.

Code (gambas)

  1. Public SimpleText As String
  2.  
  3. Public Sub Form_Open()
  4.  
  5. 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"
  6.  
  7.  
  8. Public Sub pr1_Draw()
  9.   Paint.Font = Font["Linux Libertine,12"]
  10.   Paint.DrawRichText(SimpleText, 960, 960, Paint.Width - 2 * 960)
  11.  
  12. Public Sub bPrintPlain_Click()
  13.   If pr1.Configure() Then Return 'if user clicks Cancel, don't continue
  14.   pr1.Print

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.
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
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:

Code (gambas)

  1. Paint.DrawRichText(SimpleText, 960, 960, Paint.Width - 2 * 960)
  2. E

To:

Code (gambas)

  1. 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 :


Code (gambas)

  1.   Printer1.Resolution = 64
  2.    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
Online now: No Back to the top

Post

Posted
Rating:
#7
Regular
Doctor Watson is in the usergroup ‘Regular’
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

Code (gambas)

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

Code (gambas)

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

Code (gambas)

  1. Public Sub pr1_Draw()
  2.   Paint.Font = Font["Ubuntu,12"] 'I looked it up. The Ubuntu font is on my system
  3.   Paint.DrawRichText(SimpleText, 10, 10)
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.
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#9
Regular
Doctor Watson is in the usergroup ‘Regular’
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.
Online now: No Back to the top
1 guest and 0 members have just viewed this.