Print PDF downloaded from web

Good afternoon.

I use Xojo Desktop 2018 r3 to print PDF files downloaded from the web. Currently, I am printing files using:

s.Execute "lpr " + f.ShellPath

It would be nice to display a print dialog to give the user feedback that the print job is being sent to the printer. Also, if they could choose the printer (for those who have more than one) that would be nice.

Any ideas? Thanks.

Mac? Win? Linux?

MBS Plugins comes with examples to print on Mac via PDFKit and Windows using DynaPDF.

Mac OS. Thanks.

Is there a shell command to print with a print dialog?

Here’s a function that uses AppleEvents and to print a PDF, optionally with a print dialog. Notes:
• It obviously requires to be installed.
• It will block the calling function until the user clicks Print or Cancel, or the AppleEvent times out. (See AppleEvent.Timeout in Xojo’s docs.)

Public Function PrintPDF(objFile As FolderItem, bolShowPrintDialog As Boolean) as Integer
  #If TargetMacOS Then
    Dim objEvent as AppleEvent
    Dim objDescList as AppleEventDescList
    objEvent = New AppleEvent("aevt", "pdoc", "")
    objDescList = New AppleEventDescList
    objEvent.DescListParam("----") = objDescList
    objEvent.BooleanParam("pdlg") = bolShowPrintDialog
    If objEvent.Send = True Then
      Return 0
      Return -1
    End If
    Return -1
End Function

Simply use Xojo code to print your data to pdf: the user will have to set the Print Options and click in PDF button…

And there is a virtual PDF driver on Windows 10 too. For Linux: I do not know; someone ?

Nota: that is funny, because usually people want a silent (invisible) way to print data :wink:

You can print with MBS Plugin and PDFKit classes:

[code]// select a PDF
dim f as FolderItem = GetOpenFolderItem("")
if f = nil then Return

// open PDF
dim doc as new PDFDocumentMBS(f)

// define some print setting via PrintInfo
dim PrintInfo as new NSPrintInfoMBS

// start print operation
dim printOperation as NSPrintOperationMBS = doc.PrintOperation(printinfo)

printOperation.showsPrintPanel = true
printOperation.showsProgressPanel = true

call printOperation.runOperation[/code]

see PDFDocumentMBS, NSPrintInfoMBS and NSPrintOperationMBS classes.

[quote=423122:@Emile Schwarz]Simply use Xojo code to print your data to pdf: the user will have to set the Print Options and click in PDF button…

How would that be in Xojo code? I’m not sure about that.

[quote=423080:@David Schlam]Mac OS. Thanks.

Is there a shell command to print with a print dialog?[/quote]

You should change the channel to macOS, that will be easier to find later on.

Error: I misread your question. You already have the pdf and want to print it.

f.Launch will fire your default pdf reader (usually Preview) and let the user print it (command-P or File → Print). Then the usual print process runs.

Got it. Thanks.

I’m surprised there is no simple way to hand a pdf (unmodified) off to the printer dialog. While I don’t print that often it seems clumsy that I have to run an external app to send a doc to the printer. I can and have used LPR but that will not always observe boarder setting etc…

This could work:

Or this project…

I did some experimenting and arrived at this solution. It works well, basically I treated a PDF as a Graphic and sent it to the printer instead of a Canvas.

I then get the native OSX printer dialog. Where I can make a few choices and hit print. So not as straight forward as I thought it would be, but all the examples here on the Xojo forum helped out. I would love to optimize this further by setting the proper settings in the print dialogs from my Xojo app so that a single click accomplishes all.

Dim ps As New PrinterSetup
Dim theLabel As Picture
Dim g As Graphics
Dim Scale As Double = Val(Main.ccSales.tf_Scale.Text)

// This is the actual size of the label from shipEngine
Dim Width As Double = 275 
Dim Height As Double = 432

// set the resolution to 300 DPI for printing
ps.MaxHorizontalResolution = 300
ps.MaxVerticalResolution = 300

// Load the label into a Graphic, in this case the PDF
theLabel = Picture.Open(Main.ccSales.LabelFile)

If ps.PageSetupDialog Then 
g = OpenPrinterDialog(ps, Nil)

//Draw the picture to the printer instead of canvas
g.DrawPicture(theLabel, Val(Main.ccSales.tf_XPos.Text), Val(Main.ccSales.tf_YPos.Text), Width * Scale, Height * Scale, 0, 0, Width, Height)

End If

If I need to view the label I send it to the Canvas and display it. Anybody know how to preset the Print Dialog options so that I can bypass it (meaning no data entering ) and just print?