Ver Fonte

Fix multi-page PDF printing by using PDFDocument's native print operation.

Printing via a flattened PDF and PDFView caused blank pages after the first; print the source document directly instead.

Co-authored-by: Cursor <cursoragent@cursor.com>
AhtashamShahzad1 há 3 semanas atrás
pai
commit
262689356a
1 ficheiros alterados com 10 adições e 63 exclusões
  1. 10 63
      smart_printer/PrintService.swift

+ 10 - 63
smart_printer/PrintService.swift

@@ -245,7 +245,7 @@ enum PrintService {
         let type = contentType(for: url) ?? UTType(filenameExtension: url.pathExtension) ?? .data
 
         if type.conforms(to: .pdf), let source = PDFDocument(url: url) {
-            return flattenedPDF(from: source)
+            return source
         }
 
         if type.conforms(to: .image), let image = NSImage(contentsOf: url) {
@@ -271,20 +271,6 @@ enum PrintService {
         return nil
     }
 
-    private static var activePrintContext: PrintOperationContext?
-
-    private final class PrintOperationContext {
-        let document: PDFDocument
-        let pdfView: PDFView
-
-        init(document: PDFDocument) {
-            self.document = document
-            self.pdfView = PDFView(frame: NSRect(x: 0, y: 0, width: 612, height: 792))
-            self.pdfView.document = document
-            self.pdfView.autoScales = true
-        }
-    }
-
     @discardableResult
     private static func printPDF(_ document: PDFDocument, title: String, from window: NSWindow?) -> Bool {
         guard document.pageCount > 0 else {
@@ -292,12 +278,16 @@ enum PrintService {
             return false
         }
 
-        let context = PrintOperationContext(document: document)
-        activePrintContext = context
-        defer { activePrintContext = nil }
-
         let printInfo = configuredPrintInfo()
-        let operation = NSPrintOperation(view: context.pdfView, printInfo: printInfo)
+        guard let operation = document.printOperation(
+            for: printInfo,
+            scalingMode: .pageScaleDownToFit,
+            autoRotate: true
+        ) else {
+            showPrintFailedAlert()
+            return false
+        }
+
         return runPrintOperation(operation, title: title, from: window)
     }
 
@@ -822,49 +812,6 @@ enum PrintService {
         return image
     }
 
-    private static func flattenedPDF(from source: PDFDocument) -> PDFDocument? {
-        guard source.pageCount > 0 else { return nil }
-
-        let printInfo = configuredPrintInfo()
-        let pageSize = printInfo.paperSize
-        guard pageSize.width > 0, pageSize.height > 0 else { return nil }
-
-        let pdfData = NSMutableData()
-        var mediaBox = CGRect(origin: .zero, size: pageSize)
-
-        guard let consumer = CGDataConsumer(data: pdfData as CFMutableData),
-              let context = CGContext(consumer: consumer, mediaBox: &mediaBox, nil) else {
-            return nil
-        }
-
-        for index in 0..<source.pageCount {
-            guard let page = source.page(at: index) else { continue }
-            let pageBounds = page.bounds(for: .mediaBox)
-            guard pageBounds.width > 0, pageBounds.height > 0 else { continue }
-
-            context.beginPDFPage(nil)
-            context.saveGState()
-
-            let scale = min(pageSize.width / pageBounds.width, pageSize.height / pageBounds.height)
-            let drawWidth = pageBounds.width * scale
-            let drawHeight = pageBounds.height * scale
-            let offsetX = (pageSize.width - drawWidth) / 2
-            let offsetY = (pageSize.height - drawHeight) / 2
-
-            context.translateBy(x: offsetX, y: offsetY)
-            context.scaleBy(x: scale, y: scale)
-            context.translateBy(x: -pageBounds.origin.x, y: -pageBounds.origin.y)
-            page.draw(with: .mediaBox, to: context)
-
-            context.restoreGState()
-            context.endPDFPage()
-        }
-
-        context.closePDF()
-        guard !(pdfData as Data).isEmpty else { return nil }
-        return PDFDocument(data: pdfData as Data)
-    }
-
     private static func pdfDocument(from image: NSImage) -> PDFDocument? {
         let printInfo = configuredPrintInfo()
         let pageSize = printInfo.paperSize