iOS:生成 PDF 时 UIWebView 和 Base64 编码图像的奇怪行为
<p><p>我已经设法在 <code>UIWebView</code> 中加载了以下 HTML 字符串。文本和图像均正确呈现:</p>
<pre><code><b>Hello <i>World</i>!</b><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==\"/>
</code></pre>
<p>但是,如果我在 UIWebView 上调用 <code>loadHTMLString</code> 后立即尝试从此 HTML 字符串生成 PDF,则 Base64 编码的图像不会显示在 UIWebView 中(也不会显示在 PDF 中)。</p>
<p>这是完整的代码:</p>
<pre><code>@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)")
}
</code></pre>
<p>没有警告或错误消息,这是怎么回事?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我为同样的问题苦苦挣扎了好几个小时。 <strong><em>似乎 UIMarkupTextPrintFormatter 不能正确解释 base64 图像和从本地包加载的图像。</em></strong>
我可以从 HTML 正确渲染 PDF 的唯一方法是通过 UIWebView:一旦 UIWebView 加载了你的 HTML 字符串,你必须使用它的 UIViewPrinterFormatter 作为你绘制 PDF 的函数的 printFormatter。</p>
<p>这是我的 UIWebView 的示例:</p>
<pre><code>override func viewDidLoad() {
super.viewDidLoad()
pdfRender = PdfRender()
webView.loadHTMLString(pdfRender.renderPdfCard(wine: wine), baseURL: NSURL(string: pdfRender.pathTemplatePdf!)! as URL)
}
</code></pre>
<p>PdfRender 是从带有占位符的 HTML 执行渲染的类(wine 是我的模型对象)。 <strong><em>renderPdfCard</em></strong> 方法将完整的 HTML 作为字符串返回。</p>
<p>这里要实现的 UIWebViewDelegate:</p>
<pre><code>func webViewDidFinishLoad(_ webView: UIWebView) {
if !webView.isLoading{
pdfRender.exportHTMLContentToPDF(wine: wine, printFormatter: webView.viewPrintFormatter())
}
}
</code></pre>
<p>还有一些来自类<strong><em>PdfRender</em></strong>的方法:</p>
<pre><code>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)
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
}
</code></pre>
<p>代码已更新为 Swift 3。
希望这可能会有所帮助!</p></p>
<p style="font-size: 20px;">关于iOS:生成 PDF 时 UIWebView 和 Base64 编码图像的奇怪行为,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33860041/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33860041/
</a>
</p>
页:
[1]