Forum > General

Printing to PDF [TOPIC CLOSED - NOT A LAZARUS PROBLEM]

(1/4) > >>

Badger:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, PrintersDlgs,  Printers, ExtCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Label1: TLabel;    PrintDialog1: TPrintDialog;    RadioGroup1: TRadioGroup;    procedure Button1Click(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  x,y,z:Integer;begin  If PrintDialog1.Execute then  begin    Printer.Orientation:=poPortrait;     z:=24;    x:=50;    y:=50;      Printer.BeginDoc;     Printer.Canvas.Font.Name:='Arial';     Printer.Canvas.Font.Size:=z;     Printer.Canvas.TextOut(x,y,'Test Output');    printer.EndDoc;  end;end; end.           
lpr code

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program PDF_Test; {$mode objfpc}{$H+} uses  {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}  Interfaces, // this includes the LCL widgetset  Forms, printer4lazarus, Unit1  { you can add units after this }; {$R *.res} begin  RequireDerivedFormResource:=True;  Application.Scaled:=True;  Application.Initialize;  Application.CreateForm(TForm1, Form1);  Application.Run;end.  

derek.john.evans:
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:

--- Quote ---Maybe try a different viewer?
--- End quote ---
Same result with Avast, Adobe, Chrome, Firefox.

--- Quote ---It might be easier if you provide the PDF you are having issues with.
--- End quote ---
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.

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

derek.john.evans:
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).

Navigation

[0] Message Index

[#] Next page

Go to full version