|
@@ -245,7 +245,7 @@ enum PrintService {
|
|
|
let type = contentType(for: url) ?? UTType(filenameExtension: url.pathExtension) ?? .data
|
|
let type = contentType(for: url) ?? UTType(filenameExtension: url.pathExtension) ?? .data
|
|
|
|
|
|
|
|
if type.conforms(to: .pdf), let source = PDFDocument(url: url) {
|
|
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) {
|
|
if type.conforms(to: .image), let image = NSImage(contentsOf: url) {
|
|
@@ -271,20 +271,6 @@ enum PrintService {
|
|
|
return nil
|
|
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
|
|
@discardableResult
|
|
|
private static func printPDF(_ document: PDFDocument, title: String, from window: NSWindow?) -> Bool {
|
|
private static func printPDF(_ document: PDFDocument, title: String, from window: NSWindow?) -> Bool {
|
|
|
guard document.pageCount > 0 else {
|
|
guard document.pageCount > 0 else {
|
|
@@ -292,12 +278,16 @@ enum PrintService {
|
|
|
return false
|
|
return false
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- let context = PrintOperationContext(document: document)
|
|
|
|
|
- activePrintContext = context
|
|
|
|
|
- defer { activePrintContext = nil }
|
|
|
|
|
-
|
|
|
|
|
let printInfo = configuredPrintInfo()
|
|
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)
|
|
return runPrintOperation(operation, title: title, from: window)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -822,49 +812,6 @@ enum PrintService {
|
|
|
return image
|
|
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? {
|
|
private static func pdfDocument(from image: NSImage) -> PDFDocument? {
|
|
|
let printInfo = configuredPrintInfo()
|
|
let printInfo = configuredPrintInfo()
|
|
|
let pageSize = printInfo.paperSize
|
|
let pageSize = printInfo.paperSize
|