我在从 UIWebView 创建 PDF 时遇到问题。
let render = UIPrintPageRenderer();
/* 1. pdf from webView */
render.addPrintFormatter(webView.viewPrintFormatter(), startingAtPageAtIndex: 0);
// 2. 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")
// 3. 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();
// 4. Save PDF file
print("open \(path)");
pdfData.writeToFile(path, atomically: true);
一切正常,但如果我在 HTML 中使用背景图片,它就会消失。有谁知道怎么了?