Recent

Author Topic: [SOLVED] Suggesting a FileName when printing to 'Microsoft Print To PDF'  (Read 6952 times)

fatmonk

  • Sr. Member
  • ****
  • Posts: 252
I'd like to include a suggested file name when printing to a PDF using

I found this answer on Stack Overflow that suggests a way to do this in Delphi, but fpc/lazarus doesn't have .GetPrinter() or a compatible SetPrinter() in printers.pas.

I'm not using actually the PrintDialog when printing to PDF as my document is fixed in size so I have have no need for a dialog, I am simply setting the printer and the number of copies directly on the Printer object then running through the same routine I use when printing to any other printer. (I do use the dialog when printing normally to allow the user to select the printer, though, so the dialog is available).

Code: [Select]
    Printer.SetPrinter('Microsoft Print To PDF');
    Printer.Copies:=1;
//    PrintDialog1.PrintToFile:=true;
//    Printer.FileName:=suggestFileName()+'.pdf';
    doPrint();

Everything works except for setting a filename in the dialog that pops up for saving the generated PDF.

The two commented lines are things I initially tried to set a filename (suggestFileName() returns a string with a suggetsed file name based on various parameters form the rest of the application, doPrint() contains all of the BeginDoc and EndDoc stuff as well as Printer.PaperSize.PaperName:=, Printer.Title:= etc).

When printing normally (ie not to a PDF) I am using:

Code: [Select]
  if (PrintDialog1.Execute) then doPrint();
Surely there must be a Lazarus/FPC way to acheive this if it was possible in Delphi...

Thanks,

-FM
« Last Edit: December 05, 2019, 01:11:05 pm by fatmonk »

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Suggesting a FileName when printing to 'Microsoft Print To PDF'
« Reply #1 on: December 04, 2019, 01:05:55 pm »
For me your code is working. Please see attached demo. It checks whether the "Microsoft print to PDF" printer is available, selects it and sets the filename by setting the FileName property. (When the printer is not found it opens the PrintDialog).

fatmonk

  • Sr. Member
  • ****
  • Posts: 252
Re: Suggesting a FileName when printing to 'Microsoft Print To PDF'
« Reply #2 on: December 05, 2019, 01:10:39 pm »
Wow... I have no idea why it wasn't working before...

I compared your code with mine and it looks the same, so I tried mine again and suddenly it's working.. I guess I must have been tripping thing sup with some other code somewhere.

Thanks @wp for confirming I was on the right track, though. Googling for the issue turned up a lot of discussions about it not being possible with the 'Microsoft print to PDF' device.. guess those threads were all wrong as it IS working fine now :-)

-FM

 

TinyPortal © 2005-2018