Recent

Author Topic: Printing to PDF [TOPIC CLOSED - NOT A LAZARUS PROBLEM]  (Read 2175 times)

Badger

  • Full Member
  • ***
  • Posts: 144
Printing to PDF [TOPIC CLOSED - NOT A LAZARUS PROBLEM]
« 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.  
« Last Edit: July 06, 2022, 06:26:48 am by Badger »
Badger
(A bad tempered, grumpy animal that sleeps most of the winter!)

If at first you don't succeed - you're running about average!

I'm using Windows 10 Lazarus v2.4.4  FPC 3.2.2   Win 32/64

dje

  • Full Member
  • ***
  • Posts: 134
Re: Printing to PDF
« Reply #1 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?

Badger

  • Full Member
  • ***
  • Posts: 144
Re: Printing to PDF
« Reply #2 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.
« Last Edit: July 03, 2022, 05:03:26 am by Badger »
Badger
(A bad tempered, grumpy animal that sleeps most of the winter!)

If at first you don't succeed - you're running about average!

I'm using Windows 10 Lazarus v2.4.4  FPC 3.2.2   Win 32/64

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Printing to PDF
« Reply #3 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.
The only true wisdom is knowing you know nothing

dje

  • Full Member
  • ***
  • Posts: 134
Re: Printing to PDF
« Reply #4 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).
« Last Edit: July 03, 2022, 03:43:05 am by derek.john.evans »

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Printing to PDF
« Reply #5 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.
The only true wisdom is knowing you know nothing

Badger

  • Full Member
  • ***
  • Posts: 144
Re: Printing to PDF
« Reply #6 on: July 03, 2022, 05:06:02 am »
Please see  'Update' on my previous post
Badger
(A bad tempered, grumpy animal that sleeps most of the winter!)

If at first you don't succeed - you're running about average!

I'm using Windows 10 Lazarus v2.4.4  FPC 3.2.2   Win 32/64

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Printing to PDF
« Reply #7 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.
The only true wisdom is knowing you know nothing

Badger

  • Full Member
  • ***
  • Posts: 144
Re: Printing to PDF
« Reply #8 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
« Last Edit: July 04, 2022, 08:58:24 am by Badger »
Badger
(A bad tempered, grumpy animal that sleeps most of the winter!)

If at first you don't succeed - you're running about average!

I'm using Windows 10 Lazarus v2.4.4  FPC 3.2.2   Win 32/64

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Printing to PDF
« Reply #9 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.
The only true wisdom is knowing you know nothing

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Printing to PDF
« Reply #10 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.

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Badger

  • Full Member
  • ***
  • Posts: 144
Re: Printing to PDF
« Reply #11 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
Badger
(A bad tempered, grumpy animal that sleeps most of the winter!)

If at first you don't succeed - you're running about average!

I'm using Windows 10 Lazarus v2.4.4  FPC 3.2.2   Win 32/64

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Printing to PDF
« Reply #12 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.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Printing to PDF
« Reply #13 on: July 05, 2022, 11:00:06 am »
Hi!

I have installed the PDF Creator:

https://www.pdfforge.org/


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

Winni

Badger

  • Full Member
  • ***
  • Posts: 144
Re: Printing to PDF
« Reply #14 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.
« Last Edit: July 06, 2022, 06:31:47 am by Badger »
Badger
(A bad tempered, grumpy animal that sleeps most of the winter!)

If at first you don't succeed - you're running about average!

I'm using Windows 10 Lazarus v2.4.4  FPC 3.2.2   Win 32/64

 

TinyPortal © 2005-2018