Printing to a PDF, SVG or Postscript file
Posted
#1
(In Topic #664)
Regular

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)
- ' Gambas class file
- With MyPrinter
- 'I tried :
- 'but as you see, that doesn't work.
- With fText
- .width = 1
- .height = 100
- .text = "Please add some text and print it to Output.pdf."
- With MyButton
- .width = 1
- .height = 26
- .text = "MyPrinter.Configure"
- With MyButton2
- .width = 1
- .height = 26
- .text = "Trying to print"
- .width = 300
- .padding = 20
- .Arrangement = Arrange.Vertical
- .height = fText.Height + MyButton.Height + MyButton2.height + (.Padding * 2)
- fText.SetFocus
- MyPrinter.Configure
Old african saying:
You eat an elephant one small bite at a time.
You eat an elephant one small bite at a time.
Posted
Enthusiast

Programming Gambas from Zip/Printing - Wikibooks, open books for an open world
Posted
Enthusiast

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)
- ' Gambas class file
- '' You need to add Printer1 to the FMain.form
- ' Public Printer1 As New Printer I could not make it work here
- With fText
- .width = 1
- .height = 100
- .text = "Please add some text and print it to Output.pdf."
- With MyButton
- .width = 1
- .height = 26
- .text = "Printer1.Configure"
- With MyButton2
- .width = 1
- .height = 26
- .text = "Trying to print"
- .width = 300
- .padding = 20
- .Arrangement = Arrange.Vertical
- .height = fText.Height + MyButton.Height + MyButton2.height + (.Padding * 2)
- fText.SetFocus
- Printer1.Configure()
- Paint.DrawText(fText.Text, 10, 10)
- Printer1.Configure
Posted
Enthusiast

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

First : you can add the printer like this : 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 :
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)
- 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"
- Paint.DrawRichText(SimpleText, 960, 960, Paint.Width - 2 * 960)
- 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.
You eat an elephant one small bite at a time.
Posted
Enthusiast

the listing from gambas from zip will work with qt4 as is, or it will work with GTK3 by changing the line:
Code (gambas)
- Paint.DrawRichText(SimpleText, 960, 960, Paint.Width - 2 * 960)
- E
To:
Code (gambas)
- 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)
- Printer1.Resolution = 64
- Paint.FontScale = 1
I do wish there was a standard
Posted
Regular

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)
- Paint.DrawRichText(SimpleText, X, Y, Width, Height, Alignment)
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 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.
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.
You eat an elephant one small bite at a time.
Posted
Enthusiast

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.
Posted
Regular

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



