Lazarus

Programming => General => Topic started by: Badger on July 02, 2022, 03:06:46 pm

Title: Printing to PDF [TOPIC CLOSED - NOT A LAZARUS PROBLEM]
Post by: Badger on July 02, 2022, 03:06:46 pm
I'm writing a fairly large club management application with many units.  Much of the output is directed to the user's printer.  However, if the user needs to distribute the output electronically they are advised to use  'Microsoft Print to PDF'.  This has worked up until recently when one recently-written unit seemed to print to PDF OK but produced an error 'Failed to load PDF document' when you opened the PDF file.  All the other functions still wrote to PDF and could be opened without a problem.
I have tried to find the difference between the print instructions but could not identify any problems so I wrote the attached test application.
On my machine, the print job works OK if sent to my printer from the Print Dialog but displays an error when you select Microsoft Print to PDF then try to open the PDF file.
If you comment out the three lines between begindoc and enddoc no error is generated and Chrome displays the blank page.
Could someone please try the test app to see if they get the same result.  If so, any idea what the problem is?
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, PrintersDlgs,
  9.   Printers, ExtCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Label1: TLabel;
  18.     PrintDialog1: TPrintDialog;
  19.     RadioGroup1: TRadioGroup;
  20.     procedure Button1Click(Sender: TObject);
  21.   private
  22.  
  23.   public
  24.  
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36. procedure TForm1.Button1Click(Sender: TObject);
  37. var
  38.   x,y,z:Integer;
  39. begin
  40.   If PrintDialog1.Execute then
  41.   begin
  42.     Printer.Orientation:=poPortrait;
  43.  
  44.     z:=24;
  45.     x:=50;
  46.     y:=50;
  47.  
  48.  
  49.     Printer.BeginDoc;
  50.      Printer.Canvas.Font.Name:='Arial';
  51.      Printer.Canvas.Font.Size:=z;
  52.      Printer.Canvas.TextOut(x,y,'Test Output');
  53.     printer.EndDoc;
  54.   end;
  55. end;
  56.  
  57. end.
  58.          
  59.  

lpr code
Code: Pascal  [Select][+][-]
  1. program PDF_Test;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Interfaces, // this includes the LCL widgetset
  10.   Forms, printer4lazarus, Unit1
  11.   { you can add units after this };
  12.  
  13. {$R *.res}
  14.  
  15. begin
  16.   RequireDerivedFormResource:=True;
  17.   Application.Scaled:=True;
  18.   Application.Initialize;
  19.   Application.CreateForm(TForm1, Form1);
  20.   Application.Run;
  21. end.
  22.  
  23.  
Title: Re: Printing to PDF
Post by: dje on July 02, 2022, 03:22:22 pm
It might be easier if you provide the PDF you are having issues with. So, you are viewing using Chrome. I googled "chrome Failed to load PDF document" and this popped up:

The “Failed to Load PDF Document” error message indicates that the web browser you are using, Google Chrome, is trying to open the electronic transcript within its own native PDF viewer. Because the transcript is a secured PDF, it must be opened with Adobe Acrobat Reader.

Maybe try a different viewer?
Title: Re: Printing to PDF
Post by: Badger on July 03, 2022, 03:04:55 am
Quote
Maybe try a different viewer?
Same result with Avast, Adobe, Chrome, Firefox.
Quote
It might be easier if you provide the PDF you are having issues with.
Attached.  The 'failed' file appears to be empty.
*************UPDATE:*****************
 In retrying my original program many times I found out that it saves perfectly whenever you name a new file to save the PDF to.  However, if you try to update an existing PDF, (it asks if you want to overwrite and you click 'Yes') the resultant file is empty. 
The astonishing thing is that the same thing happened with a 'commercial' program like Libre Office.  Surely it must be my installation of Microsoft Print to PDF.  Microsoft would have found this out by now!
Could someone please use Microsoft Print to PDF to save any file and display it then try to save the same file under the same name and see if it saves and loads.
Title: Re: Printing to PDF
Post by: jamie on July 03, 2022, 03:19:59 am
Did you try giving it a NewPage at the End or start ?

Normally to complete a sheet of paper you need a NewPage.

I can't say if that will help but if could be hung up in limbo waiting for the end.
Title: Re: Printing to PDF
Post by: dje on July 03, 2022, 03:40:44 am
Interesting. For the simple test app not to work, this feels like either a corrupted Lazarus, a new bug in an updated Lazarus version, or a wacky MS bug/permissions/update/64bit issue.

Considering there are a number of reports of PDF printing failing on Windows, this might be a MS issue:
https://www.google.com/search?client=firefox-b-e&q=+Microsoft+Print+to+PDF+fails

Only you know the changes made to your system between the time it "worked" and now.

1) When you say "why do most units in my original app" do you mean an old compilation?
2) Or are you saying your original app works even when recompiled to a fresh exe?
3) Have you changed Lazarus versions lately?
4) Do you have another PC to test this on?
5) What version of Windows/Lazarus are you running?

All I can say is, your code works fine on a Raspberry Pi (Lazarus 2.0.10) with the cups pdf printer (ATTACHED PDF).
Title: Re: Printing to PDF
Post by: jamie on July 03, 2022, 03:46:07 am
I just tried my own simple test run basically what they have here and it works just fine.

 I don't know what to say.

 I was kind of concerned for a moment because I too use that feature for a small util app.
Title: Re: Printing to PDF
Post by: Badger on July 03, 2022, 05:06:02 am
Please see  'Update' on my previous post
Title: Re: Printing to PDF
Post by: jamie on July 03, 2022, 12:14:56 pm
Please see  'Update' on my previous post
Yes I I used the same file name multiple times and I used the document folder.
It had problem over writing the file.
Maybe you have a permission issue or maxed out storage in a folder?
 I remember years ago win98 and 95 would do that in some system folders.
Title: Re: Printing to PDF
Post by: Badger on July 04, 2022, 04:28:28 am
Quote
Did you try giving it a NewPage at the End or start ?
No - I hate 'work arounds' anyway it works fine on a new file.
Quote
maxed out storage in a folder?
Tried new folder in Documents and printed a Notepad file 6 times to differently named files - all worked.  I then removed all but the first file and tried to overcopy it and it failed.  I don't think permissions is the problem either because it saves to newly named files.
I'm seriously thinking it's a 'Microsoft Print to PDF' problem, either a bug or something corrupted in my version.

Could someone with Windows 10 please:-
1 Open a file in Notepad
2 Print it after selecting Microsoft Print to PDF
3 Save PDF file to a folder you can remember
4 Open the PDF file and ascertain that it opens correctly (it should)
5 Go back to the Notepad File and save it again to the same folder (click on the old version to enter it into the File Name - so it is exactly the same name)
6 Save the file ( you will be asked if you wish to over-write the old file - click yes)
7 Try to open the PDF file and see if it works. (It gives an error on my machine)
8 Let me know the result on this thread.

If people get it working OK I'll have to start looking at my Print to PDF - I'll probably start that now anyway.

Sorry, this topic seems to have strayed outside the Lazarus theme
Title: Re: Printing to PDF
Post by: jamie on July 04, 2022, 12:20:49 pm
I did my test on Win10 Pro and Home and it works.

 If you have the previous file open from some viewer you will most likely have issues trying to replace it.
Title: Re: Printing to PDF
Post by: J-G on July 04, 2022, 03:10:59 pm
If you have the previous file open from some viewer you will most likely have issues trying to replace it.
I suspect that that is the problem - -  there ought to be another step between 4) & 5)   -  CLOSE the .PDF file.

Title: Re: Printing to PDF
Post by: Badger on July 05, 2022, 08:37:47 am
I did miss out that instruction but have actually been closing the PDF file.  It doesn't work either way
Title: Re: Printing to PDF
Post by: SymbolicFrank on July 05, 2022, 09:13:50 am
Printing on Windows is iffy since the Universal printing update years ago. Lots of problems. The most frustrating one: you are not allowed to print on A4 if you install a printer while you have US components in any regional settings, like a US-international keyboard. And they don't feel like fixing them.

Short list. (https://docs.microsoft.com/en-us/universal-print/fundamentals/universal-print-known-issues)
Title: Re: Printing to PDF
Post by: winni on July 05, 2022, 11:00:06 am
Hi!

I have installed the PDF Creator:

https://www.pdfforge.org/ (https://www.pdfforge.org/)

It is a GhostScript wrapper amd works without problems.
Free for private use.

Winni
Title: Re: Printing to PDF
Post by: Badger on July 06, 2022, 06:23:09 am
Quote
I have installed the PDF Creator:https://www.pdfforge.org/
I shall have a look at it.

My reason for opening the original post was that I thought there was something wrong with my Lazarus code.

From the answers I have received from this forum and friends who have done the above tests, it is obvious that the problem is in my MS Print to PDF file/Windows files.  Seeing that it is almost certainly not a Lazarus problem, I will close this topic. 

To fix my computer, I think I'll have to reinstall my defective Print to PDF file.  So far I haven't managed to find a way to do this - everything I read tells me how to get Print to PDF working - nothing on how to download and reinstall actual Print to PDF file.
 
The last thing I want to do is reinstall Windows.  It took me ages to get it working satisfactorily last time.

I have put a query in to Microsoft Community but so far have only received one stupid answer which said printing to a PDF was a printer manufacturer thing.  So I'm not holding my breath.
Title: Re: Printing to PDF
Post by: rvk on July 06, 2022, 09:41:27 am
To fix my computer, I think I'll have to reinstall my defective Print to PDF file.  So far I haven't managed to find a way to do this - everything I read tells me how to get Print to PDF working - nothing on how to download and reinstall actual Print to PDF file.
Can't you just deselect "Print to file" in the optionalfeatures.exe? REBOOT. Check if the printer is gone. Run optionalfeatures.exe again and select "Microsoft Print to file" again?

I tried it here in both production Windows 10 Pro and Windows VM and it worked fine saving under the same pdf.

One thing you could try is to print to pdf, REBOOT, and then again try to print to the same pdf. If that works there could be some process in play (virusscanner, explorer.exe) which holds the file (although I would expect an error then).

Anyway... I NEVER advise "Microsoft Print to PDF" because it results in a much larger PDF file than all the other Print to PDF solutions. MS Print to File is notorious for creating a very inefficient PDF. So I use my own solution in combination with ghostscript (much like PdfForge, PdfCreator, Bullzip and all other free solutions).

But your problem with MS Print to PDF is still very interesting.
Title: Re: Printing to PDF [TOPIC CLOSED - NOT A LAZARUS PROBLEM]
Post by: Badger on July 07, 2022, 03:01:19 am
Quote
Can't you just deselect "Print to file" in the optionalfeatures.exe? REBOOT. Check if the printer is gone. Run optionalfeatures.exe again and select "Microsoft Print to file" again?

It worked!!!! Why didn't I think of that??? Ah Yes, rvk is a Hero Member and I'm just a lowly wannabe :D

I have taken advice and installed winni's PDF Creator

Thanks everyone for your help.
TinyPortal © 2005-2018