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

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

问题描述

  / ** 
* $ b在swift 3.0中创建PDF文件$ b * /
func exportHtmlContentToPDF(HTMLContent:String,filePath:String){
//让webView = UIWebView(frame:CGRect(x:0,y:0,width:694,height:603 ));

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

let pdfPrinter = PDFPrinter();
让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();

返回数据;
}

除了我的base64编码图像以外,一切都很好。 HTML内容本身在web视图或Safari浏览器或Chrome浏览器中正确显示,并正确显示所有图像。但是这些图像永远不会呈现在PDF中。



为什么图像不能渲染,我怎么才能渲染它们?


<我找到了解决方案!



导出到PDF发生在渲染过程完成之前。如果您放入一张非常小的图片,它会显示在PDF中。如果图片太大,则渲染过程需要太多时间,但PDF导出不会等待渲染完成。



所以我做了以下工作:

在导出为PDF之前,我显示WebView中的HTML结果。 WebView正在渲染所有内容,现在当我按下导出为PDF时,PDF正在正确显示所有图像。



所以我想这是一个巨大的延迟没有办法告诉PDF Exporter等待渲染过程完成。


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;
}

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?

解决方案

I found the solution!

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.

So what I did to make it work is the following:

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.

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天全站免登陆