我设法在UIWebView
中加载以下HTML字符串。文本和图像都正确呈现:
<b>Hello <i>World</i>!</b><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==\"/>
但是,如果我在UIWebView上调用loadHTMLString
后立即尝试从此HTML字符串生成PDF,则Base64编码的图像不会显示在UIWebView中(也不会显示在PDF中)。
这是完整的代码:
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let htmlString = "<b>Hello <i>World!</i></b><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==\"/>"
// load HTML string into the UIWebView
// works correctly if the code after this line is commented
webView.loadHTMLString(htmlString, baseURL: nil)
/* Generate PDF and save it to file. */
let fmt = UIMarkupTextPrintFormatter(markupText: htmlString)
let render = UIPrintPageRenderer()
render.addPrintFormatter(fmt, startingAtPageAtIndex: 0)
let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8)
render.setValue(NSValue(CGRect: page), forKey: "paperRect")
let printable = CGRectInset(page, 0, 0)
render.setValue(NSValue(CGRect: printable), forKey: "printableRect")
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 path = "\(NSTemporaryDirectory())file.pdf"
pdfData.writeToFile(path, atomically: true)
print("open \(path)")
}
没有警告或错误消息,所以发生了什么?
答案 0 :(得分:1)
我在同样的问题上花费了很多时间。 似乎UIMarkupTextPrintFormatter无法正确解释base64图像,也无法正确解释从本地软件包加载的图像。 我能从HTML中正确呈现PDF的唯一方法是通过UIWebView:一旦UIWebView加载了你的HTML字符串,就必须使用它的UIViewPrinterFormatter作为绘制PDF的函数的printerFormatter。
这是我的UIWebView的一个例子:
override func viewDidLoad() {
super.viewDidLoad()
pdfRender = PdfRender()
webView.loadHTMLString(pdfRender.renderPdfCard(wine: wine), baseURL: NSURL(string: pdfRender.pathTemplatePdf!)! as URL)
}
PdfRender是一个从带有占位符的HTML执行渲染的类(wine是我的模型对象)。方法 renderPdfCard 将完整的HTML作为字符串返回。
这里要实现UIWebViewDelegate:
func webViewDidFinishLoad(_ webView: UIWebView) {
if !webView.isLoading{
pdfRender.exportHTMLContentToPDF(wine: wine, printFormatter: webView.viewPrintFormatter())
}
}
在这里,来自 PdfRender 类的一些方法:
func exportHTMLContentToPDF(wine: Wine, printFormatter: UIViewPrintFormatter) {
let printPageRenderer = CustomPrintPageRenderer()
printPageRenderer.addPrintFormatter(printFormatter, startingAtPageAt: 0)
let pdfData = self.drawPDFUsingPrintPageRenderer(printPageRenderer: printPageRenderer)
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
pdfFilename = "\(documentsPath)/test.pdf"
pdfData?.write(toFile: pdfFilename, atomically: true)
print(pdfFilename)
}
func drawPDFUsingPrintPageRenderer(printPageRenderer: UIPrintPageRenderer) -> NSData! {
let data = NSMutableData()
UIGraphicsBeginPDFContextToData(data, CGRect.zero, nil)
UIGraphicsBeginPDFPage()
printPageRenderer.drawPage(at: 0, in: UIGraphicsGetPDFContextBounds())
UIGraphicsEndPDFContext()
return data
}
代码更新为Swift 3。 希望这可能有所帮助!