How to Print An Existing PDF to Paper

Let’s assume I don’t want to print TO pdf … rather, I already have a pdf document which I wish to print out onto some actual paper. My users like dead tree carcasses…

How can I go about printing a PDF to paper using the familiar printer dialog box for the user? Needs to be cross platform desktop Windows/Mac.

I’ve tried loading the pdf into an HTMLViewer instance and printing that, but it does not format/print correctly. Other approaches? I would prefer not to have to open the PDF in some 3rd party app and make the user print from there. Ideally:

User presses a “print” button in my app. Printer dialog box appears where they select printer, set paper size, etc. They press print there, and paper starts shooting out of the printer with their PDF on it.

Is this even possible? Plugins are welcome, if that’s what I need.

PDFKit for Mac and DynaPDF for Windows in MBS Xojo Plugins are available.

Hi Christian - your PDFKit for OS X looks awesome, and since I already own that plugin I’ll likely go that route for OS X. However, on Windows, your example for DynaPDF seems to just leverage Acrobat Reader for printing, and just fails to print if the user does not have Acrobat installed. Is this really how DynaPDF addresses printing? Is there no built-in way to print if I purchase your DynaPDF plugin?

Here’s the example I’m referring to: https://www.monkeybreadsoftware.net/example-dynapdf-createandprintpdf.shtml

DynaPDFMBS class got a print function years ago.

Many years ago I did this on Windows by controlling Adobe Reader through shell commands. I don’t know if those still exist/work currently though. I’ve also used Sumatra PDF on Windows through shell commands. In both cases it was seamless, with the user never having to interact with anything other than my program.

I’ve never tried on a Mac before, but I believe you could print the PDFs directly with the lpr command through a shell.

Thank you Anthony! It appears that Sumatra is GPLv3, and my understanding is that would prohibit me from including it (er, installing it) as a part of my application (which is closed-source). I’d prefer not to have to ask the user to go and install some 3rd party app themselves (either Adobe Reader or Sumatra) if I can avoid it, but if it comes to it I will. Thanks!

Hi Christian - I see the DynaPDF.PrintPDFFileWithDialog() method, which may be all I need. If I’m not actually generating any PDF content with DynaPDF at all - merely printing - which license do I have to purchase?

Thanks!

i open pdf this way so user have a preview and can choose what he like to print.

Function MenuManual() As Boolean
  Var f As FolderItem = SpecialFolder.Resources.Child("Manual.pdf")
  
  If f.Exists Then 
    f.Open
  Else
    MessageBox DEEN("Fehler: Anleitung nicht gefunden","error: manual file not found")
  End If
  
  Return True
  
End Function

Print in DynaPDF needs the render engine, so it’s a Pro license.

Our older examples show how to use Adobe Reader to print on Windows, but that is not as possible to automate as if we print with DynaPDF directly.

Older example with Adobe Reader:
https://www.monkeybreadsoftware.net/example-dynapdf-createandprintpdf.shtml

Newer example with print via DynaPDF:
https://www.monkeybreadsoftware.net/example-dynapdf-printpdf.shtml

See print functions in DynaPDFMBS class:

method PrintGetDevMode as String
method PrintGetDevNames(byref Driver as String, byref Device as String, byref Output as String, byref DefaultFlag as Integer) as Boolean
method PrintPDFFile(TmpDir as FolderItem, DocName as string, DCHandle as integer, Flags as Integer = 0, Margin as DynaPDFRectMBS = nil, PrintParams as DynaPDFPrintParamsMBS = nil) as Boolean
method PrintPDFFile(TmpDir as FolderItem, DocName as string, PrinterName as String, Flags as Integer = 0, Margin as DynaPDFRectMBS = nil, PrintParams as DynaPDFPrintParamsMBS = nil) as Boolean
method PrintPDFFileWithDialog(TmpDir as FolderItem, DocName as string, Flags as Integer = 0, Margin as DynaPDFRectMBS = nil, PrintParams as DynaPDFPrintParamsMBS = nil, parentWindow as Window = nil) as Boolean
method PrintPDFPage(PageNum as Integer, DocName as string, DCHandle as integer, Flags as Integer = 0, Margin as DynaPDFRectMBS = nil, PrintParams as DynaPDFPrintParamsMBS = nil) as Boolean
method PrintPDFPage(PageNum as Integer, DocName as string, PrinterName as String, Flags as Integer = 0, Margin as DynaPDFRectMBS = nil, PrintParams as DynaPDFPrintParamsMBS = nil) as Boolean
method PrintPDFPageWithDialog(PageNum as Integer, DocName as string, Flags as Integer = 0, Margin as DynaPDFRectMBS = nil, PrintParams as DynaPDFPrintParamsMBS = nil, parentWindow as Window = nil) as Boolean
method PrintSetDevMode(data as MemoryBlock) as Boolean
method PrintSetDevMode(data as String) as Boolean
method PrintSetDevNames(Driver as String, Device as String, Output as String, DefaultFlag as Integer) as Boolean