UIMarkupTextPrintFormatter 从不呈现 base64 图像 [英] UIMarkupTextPrintFormatter never renders base64 images

查看:21
本文介绍了UIMarkupTextPrintFormatter 从不呈现 base64 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 swift 3.0 中的 html 内容创建一个 pdf 文件:

Im creating a pdf file out of html content in swift 3.0:

/**
 *
 */
func exportHtmlContentToPDF(HTMLContent: String, filePath: String) {
    // let webView = UIWebView(frame: CGRect(x: 0, y: 0, width: 694, height: 603));

    // webView.loadHTMLString(HTMLContent, baseURL: nil);

    let pdfPrinter = PDFPrinter();
    let printFormatter = UIMarkupTextPrintFormatter(markupText: HTMLContent);
    // let printFormatter = webView.viewPrintFormatter();

    pdfPrinter.addPrintFormatter(printFormatter, startingAtPageAt: 0);

    let pdfData = self.drawPDFUsingPrintPageRenderer(printPageRenderer: pdfPrinter);

    pdfData?.write(toFile: filePath, atomically: true);
}

/**
 *
 */
func drawPDFUsingPrintPageRenderer(printPageRenderer: UIPrintPageRenderer) -> NSData! {
    let data = NSMutableData();

    UIGraphicsBeginPDFContextToData(data, CGRect.zero, nil);

    printPageRenderer.prepare(forDrawingPages: NSMakeRange(0, printPageRenderer.numberOfPages));

    let bounds = UIGraphicsGetPDFContextBounds();

    for i in 0...(printPageRenderer.numberOfPages - 1) {
        UIGraphicsBeginPDFPage();

        printPageRenderer.drawPage(at: i, in: bounds);
    }

    UIGraphicsEndPDFContext();

    return data;
}

除了我的 base64 编码图像外,一切都很好.Web 视图中或 safari 或 chrome 浏览器中的 HTML 内容本身正确显示,并且正确显示所有图像.但图像永远不会渲染到 pdf 中.

Everything is rendered fine except my base64 encoded images. The HTML content itself in a webview or inside safari or chrome browser is presented correctly and is showing all images correctly. But the images are never rendered into the pdf.

为什么没有渲染图像,如何才能渲染它们?

Why are the images not rendered and how can I get them to be rendered?

推荐答案

我找到了解决方案!

在渲染过程完成之前导出为 PDF.如果您放入一张非常小的图片,它会显示在 PDF 中.如果图片太大,渲染过程会花费太多时间,但 PDF 导出不会等待渲染完成.

The export to PDF happens before the rendering process is finished. If you put in a very small picture it is showing up in the PDF. If the picture is too big the rendering process takes too much time but the PDF export isnt waiting for the rendering to finish.

所以我做了以下工作:

在导出为 PDF 之前,我在 WebView 中显示 HTML 的结果.WebView 正确呈现所有内容,现在当我按下导出为 PDF 时,PDF 正确显示,其中包含所有图像.

Before I export to PDF I show the Result of the HTML in a WebView. The WebView is rendering everything correctly and now when I press on export to PDF the PDF is showing up correctly with all images inside.

所以我想这是一个巨大的延迟,无法告诉 PDF 导出器等待渲染过程完成.

So I guess this is a huge lag that there is no way to tell the PDF Exporter to wait for the rendering process to finish.

这篇关于UIMarkupTextPrintFormatter 从不呈现 base64 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆