1
我正在從圖像的HTML文件創建PDF。我嘗試通過從Html以及從UIWebView通過將HTML加載到Web視圖創建。以下是使用方法兩種方法PDF從Html使用UIViewPrintFormatter創建不顯示本地圖像
1.從HTML
func exportHTMLContentToPDF(HTMLContent: String) {
let printPageRenderer = CustomPrintPageRenderer()
let printFormatter = UIMarkupTextPrintFormatter(markupText: HTMLContent)
printPageRenderer.addPrintFormatter(printFormatter, startingAtPageAtIndex: 0)
let pdfData = drawPDFUsingPrintPageRenderer(printPageRenderer)
pdfData.writeToFile(pdfPath(), atomically: true)
}
2.從一個UIWebView通過加載HTML
func createPdfFromViewFormatter(viewFormatter:UIViewPrintFormatter) {
let printPageRenderer = CustomPrintPageRenderer()
printPageRenderer.addPrintFormatter(viewFormatter, startingAtPageAtIndex: 0)
let pdfData = drawPDFUsingPrintPageRenderer(printPageRenderer)
pdfData.writeToFile(pdfPath(), atomically: true)
}
PDF創建method`
private func drawPDFUsingPrintPageRenderer(printPageRenderer: UIPrintPageRenderer) -> NSData! {
let data = NSMutableData()
UIGraphicsBeginPDFContextToData(data, CGRect(x: 0.0, y: 0.0, width: printPageRenderer.printableRect.width, height: printPageRenderer.printableRect.height), nil)
printPageRenderer.prepareForDrawingPages(NSRange(location: 0, length:printPageRenderer.numberOfPages()))
for i in 0..<printPageRenderer.numberOfPages() {
UIGraphicsBeginPDFPage()
printPageRenderer.drawPageAtIndex(i, inRect: UIGraphicsGetPDFContextBounds())
}
UIGraphicsEndPDFContext()
return data
}
問題是html中的圖像未在創建的pdf中顯示,而是在將其加載到UIWebView中時顯示。