I have a mac app using swiftUI framework. I get a pdf url from an api and I need to allow the user to click a button that prints the pdf.
I have it working as of right now, The alignment of the pdf is just off. What I mean is it looks like a pasted a pdf on top of another pdf so my margins are bigger than normal and I have a weird outline of the url pdf on the printed pdf.
To my understanding I can out of the box use UIKit in a mac app to use the UIPrintInteractionController.
I am using the NSPrintOperation right now.
import SwiftUI
import PDFKit
import AppKit
struct ContentView: View {
let pdfDocumentUrl = URL(string: "https://www.soundczech.cz/temp/lorem-ipsum.pdf")!
var body: some View {
VStack {
Button("Print PDF") {
let pdf = PDFDocument(url: pdfDocumentUrl)!
printPDFDocumentt(pdf)
}
}
.padding()
}
func printPDFDocumentt(_ document: PDFDocument) {
let pdfView = PDFView(frame: .init(x: 0, y: 0, width: document.page(at:
0)?.bounds(for: .mediaBox).width ?? 0,
![]("https://developer.apple.com/forums/content/attachment/5554c20f-7247-4300-b0b1-1f7a7ab6ebbe" "title=CleanShot 2023-05-09 at 11.46.25@2x.png;width=1528;height=2040")
height: document.page(at: 0)?.bounds(for: .mediaBox).height ?? 0))
pdfView.document = document
pdfView.backgroundColor = .clear
let printInfo = NSPrintInfo.shared
printInfo.horizontalPagination = .fit
printInfo.verticalPagination = .fit
printInfo.orientation = .portrait
printInfo.topMargin = 0
printInfo.bottomMargin = 0
printInfo.leftMargin = 0
printInfo.rightMargin = 0
let printOperation = NSPrintOperation(view: pdfView, printInfo: printInfo)
printOperation.run()
}
}
Post
Replies
Boosts
Views
Activity
I am using SwiftUI to build a macOS app. I have to print papers and I'm trying to see if I'm actively connected to a printer. I have a default printer set as you can see in the below image but it's currently offline. My issue is the code I have returns true that I am connected to a printer because I have a saved printer device. Is there a way to check if the printer is offline even when I'm connected?
In the image it says the printer is offline and I need to know how to get that.
Code I'm currently using that returns true:
func isConnectedToPrinter() -> Bool {
let printers = NSPrinter.printerNames
return !printers.isEmpty
}
This returns true because the printer is still remembered by my mac even though its Offline(powered down).
Any idea on how mac OS can determine the printer is "Offline"?
Also here is my current code to print the pdfDocument, Is there anything I can add here to help?
private func printPDFDocument(forDoc document: PDFDocument) {
let printInfo = NSPrintInfo.shared
printInfo.horizontalPagination = .fit
printInfo.verticalPagination = .fit
printInfo.orientation = .portrait
printInfo.topMargin = 0
printInfo.bottomMargin = 0
printInfo.leftMargin = 0
printInfo.rightMargin = 0
printInfo.isHorizontallyCentered = true
printInfo.isVerticallyCentered = true
let scale: PDFPrintScalingMode = .pageScaleDownToFit
let printOp = document.printOperation(for: printInfo, scalingMode: scale, autoRotate: true)
DispatchQueue.main.async {
let result = printOp?.run()
self.showLoadingText = false
}
}
Error: 'Runningboard has returned error 5'
I am able to run my unit tests locally just fine. When I run them to from xcode cloud It fails everytime. I did notice in app store connect that it gives me a warning 'Grant access to the following repositories to allow them to be used for this product.' as I am using google sign in iOS(https://github.com/google/googlesignin-ios.git). However I cannot grant this access as I do not have privileges to google code base. I have imported the google sign in via swift package manager and everything works fine.
This is the report from xcode cloud if it helps. Happy to add any additional info that will help