Printing on MacOS and Windows issues and advices

Greetings,

I wrote a small app that prints cheques, now because i have several issues with those reports and i did not manage it to use it properly i did all via the graphics method and printed on exact locations.

Now, the issue is the following .

I have several printers that we use and i have a cheque laser printer that is dedicated for that , it has a front loader specially for this.

I need to be able to do the Initial setup, setting the paper size, paper orientation, printer and then save those in the app settings and each time i press print , the app should select the proper printer and fill the cheque .

Currently i did a partial setup by doing the printer setup and saving the page settings on a desktop file and then on the app open i check if that file exists then i load the page settings , otherwise i ask the user to do the printer setup.
This works some times and some times not

I do something like that :

printer = New PrinterSetup

If printer.ShowPageSetupDialog Then
pageSettings = EncodeHex(printer.Settings)

 If pageSettingsFile <> Nil And pageSettingsFile.Exists = False Then
    Var tos As TextOutputStream
    
    tos = TextOutputStream.Create(pageSettingsFile)
    tos.Write(pageSettings)
    tos.Close
    
  End If
  
End If

then on the print button i use this

Var g As Graphics

If printer <> Nil Then
  printer.Settings = DecodeHex(pageSettings)
  
  g = printer.ShowPrinterDialog

'and the rest of the code to print 

One more thing, apparently i setup the coordinates for Windows and it prints perfectly, when i use same code, same printer on MacOS it shifts all the lines down with several lines.

Here , it loads the printer settings from the pageSettings property and then it asks me to select the printer on which i want to print.

on the initial setup it seems that it chooses some default system printer settings then i save all, and on the printing step it seems that once i select the printer it loads the settings properly as long as the app is open as it takes them from that property, then once i close the app and i reopen it, it looses all the settings despite having all the printer settings loaded from the file and i need to set them up again and select the printer and print.

So is there a way to do the setup, save all, then just press print and prints directly without selecting any printer ?

If yes, then how do i do that on MacOS and on Windows.

Thanks again.

The printer drivers for Mac and Windows are very different. It is likely that you need to keep a Windows version and a separate Mac version of the setup.

Hello Ian, I do that, tests are done individually for each OS, and to avoid issues, settings are saved per device. so once the app starts for the first time it saves that.

I don’t suppose you are using chr(13)+chr(10) on any of the printing. That only applied on Windows. MacOS should just be chr(10). Perhaps that makes a difference?

on which part are you talking here ? if related to the shifting , no i don’t use any of those, the issue it seems to happen with the same code and the shift is like 5, 6 pixels more down on MacOS than on Windows.

As for the printing part if i use the PrinterSetup.OpenPrinter() then it will select the last printer used , in my case it is not ok as they are using those a lot.

If would be nice if we had an option to list all the printers and then select the needed one and save it as default in the app, for the app, no idea if that is possible but i wish it could do that.

Well, the short answer is, Xojo sucks for printing…

to deal with printing in xojo, I use one of 2 options:

  • Use API calls to send print jobs directly to the printer.
  • Create and open a PDF and let the user do the printing.

For your case, I use the first option, there are examples in the forum for raw printing: Receipt Printer Raw Data

oh, you can do that with the other printers ? i mean i do that for my current thermal printer but i did not knew that you can do same for a normal printer . i guess it would help to get more details on that side.

For the thermal printer i have a headache as i did not managed until now to make it print graphics so far is super complicated it seems but the rest works well.

and apparently i did found a solution, select the printer needed, then save the printer setup on a file, then call that file directly and done, i just did a test with both options , selecting the printer and printing directly to the needed printer with the settings file, even if there is other printer selected as default.

And indeed printing is a nightmare on XOJO, specially using those reports, so i prefer to control the code.

It was nice if instead of hardcoding the location of each field if i could have it dynamically and with few interface tweaking i could have it done dynamically but i guess i could have that done if i struggle little bit , i just need to find out how i can draw interactive labels on the canvas and be able to move them live.

I tried that some years ago. Back then it worked fine for some time, then at random stops working until you delete the saved settings and create a new file. Hard to explain to the user, if the app stops printing, delete the file and repeat the printer setup :man_facepalming:t2:.

i think it has something to do with the file itself, on my side to avoid issues i use hex to encode it on saving and decode it on loading , in this way i make sure that the config is not altered, but i guess you are right, windows could change things or macos and then you need to redo the setup again.

Yeah, in my case I used base64 and once created, the file was was read only…

Yes, looks like the config string is OS dependant. Xojo should have their own multiplatform config mechanism and translate to the OS one on the fly.

But the marketing slogan of “Xojo abstracts you from the platform details allowing you to focus your time and energy on what makes your app unique.” is just a big fat lie.

1 Like

base64 could mess things around and it might be affected by encoding, i would stick to hex and it should be ok

It better well not. Base64 is not lossy! And besides, the setup strings are binary data, their encoding should be (and stay) Nil.

2 Likes

The characters used in Base64 are all within the ASCII range and thus are not subject to encoding errors if converted to UTF-8 or most of the old Latin codepages.

so, do you mean encoding it in HEX will do any harm ?

Nope, just wastes more bytes for the same binary output

well, considering the size and what it does, i don’t care that much for few bytes .
Thanks

No one emitted an opinion about your choice, except that I see it was necessary to explain that it is a lossless encoding with a bit of advantage over hex.