我目前正在尝试通过 AirPrint 从我的 iPad 打印 PDF。当我以 A4 尺寸和纵向直接打印 PDF 时,一切正常。
但是,现在我希望打印的 PDF 为 A5 大小并且是纵向的,就像我现在无法制作的 A4 纸大小的一半。任何人都对此有所了解,要么我应该调整 PDF 文件的大小(不是最好的),要么有其他方法可以做到这一点
我正在使用UIPrintInteractionController
它,它是打印的代表,而源来自viewPrintFormatter
加载 PDF 的 webView。
页面渲染器:
let fmt = webView.viewPrintFormatter()
// 2. Assign print formatter to UIPrintPageRenderer
let render = UIPrintPageRenderer()
render.addPrintFormatter(fmt, startingAtPageAtIndex: 0)
// 3. Assign paperRect and printableRect
let page = CGRect(x: 0, y: 0, width:595.2, height: 841.8) // A4, 72 dpi
let printable = CGRectInset(page, 0, 0)
render.setValue(NSValue(CGRect: page), forKey: "paperRect")
render.setValue(NSValue(CGRect: printable), forKey: "printableRect")
// 4. Create PDF context and draw
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil)
for i in 1...render.numberOfPages() {
UIGraphicsBeginPDFPage();
let bounds = UIGraphicsGetPDFContextBounds()
render.drawPageAtIndex(i - 1, inRect: bounds)
}
UIGraphicsEndPDFContext();
然后将其保存为物理文件:
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let url = NSURL(fileURLWithPath: "\(documentsPath)/file.pdf")
pdfData.writeToFile("\(documentsPath)/file.pdf", atomically: true)
最后打印出来:
if UIPrintInteractionController.canPrintURL(url) {
let printInfo = UIPrintInfo(dictionary: nil)
printInfo.jobName = url.lastPathComponent!
printInfo.outputType = .Grayscale
printInfo.orientation = .Landscape
let printController = UIPrintInteractionController.sharedPrintController()
printController.printInfo = printInfo
printController.showsPaperSelectionForLoadedPapers = true
printController.showsNumberOfCopies = true
printController.delegate = self
printController.printingItem = url
printController.presentAnimated(true, completionHandler: nil)
}