UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Printing PDF loaded from web service

Forums > macOS

Good afternoon (o;

Seems that there is no thread or article about using printing on macOS or Swift in general....nobody prints anything anymore and just upload to a Cloud? (o;

Anyway....for my posting barcode app I load a preview of the barcode from Swiss Post to make sure everything is set up correctly...

Then I could load a final PDF rendering of the barcode with an actual tracking number attached....dunno if PDFKit would allow me to load that PDF from a web service directly...well my webservice actually as the original SOAP XML is a nightmare...and the new service requires OAuth2.

And then how would I go along to bring up a printing dialog with the loaded PDF?

thanks in advance richard

3      

Well...looks like even displaying a local PDF file in SwiftUI isn't easy...and almost all examples are for iOS....

Tried code snippets from here:

No joy....but also doesn't thrown an error/warning:

import SwiftUI
import PDFKit

struct PDFKitRepresentedView: NSViewRepresentable {

  var url : URL
  func makeNSView(context: Context) -> PDFView {
    let pdfView = PDFView()
    pdfView.document = PDFDocument(url:url)
    return pdfView
  }

    func updateNSView(_ nsView: PDFView, context: Context) {  }
}

struct ContentView: View {
    let url : URL? = URL(string: "/Users/klingler/Documents/example.pdf")

    var body: some View {
        let screenWidth = NSScreen.main?.visibleFrame.size.width ?? 0.0
        let screenHeight = NSScreen.main?.visibleFrame.size.height ?? 0.0
        VStack {
            Text("Hello, world!")
                .padding()
            PDFKitRepresentedView(url:self.url!).frame(width: screenWidth * 0.3, height: screenHeight * 0.5)
        }
    }
}

3      

Ah wait....this works for remote files only ;-)

Now probably I have to load the PDF into a separate view and use that for printing....

3      

Good morning (o;

Okay..this loads a PDF remotely and shows the preview in the printing dialog:

let printOpts: [NSPrintInfo.AttributeKey: Any] = [NSPrintInfo.AttributeKey.copies: 1, NSPrintInfo.AttributeKey.printerName:"Brother QL-1060N", NSPrintInfo.AttributeKey.paperName:"Post"]
let printInfo = NSPrintInfo(dictionary: printOpts)

let pdfView = PDFView()
pdfView.document = PDFDocument(url: self.url!)
pdfView.print(with: printInfo, autoRotate: false)

But following doesn't work:

  • Pressing print does nothing
  • Can't cancel printing dialog
  • No paper selection option

The print dialog just stays forever.....

3      

Looks like no one here is using printing on macOS...

Seems it needs a window:

let pdfView = PDFView()
pdfView.document = PDFDocument(url: self.url!)

let wnd = NSWindow()
pdfView.autoScales = true
pdfView.displaysPageBreaks = false
wnd.setContentSize(pdfView.frame.size)
wnd.contentView = pdfView

pdfView.print(with: printInfo, autoRotate: false)

Though this popups the printing dialog in the lower left corner....and when choosing "details" the window extends outside the screen...

And still no paper size selection....

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.