我有一个html文件,其中包含静态和动态数据,我需要从该html文件生成PDF文件.我能够创建PDF文件,但它只打印第一页.我需要一个三页的PDF文件.我使用了以下两个函数来绘制和保存PDF文件.我的代码是:
func exportHTMLContentToPDF(HTMLContent: String) { let printFormatter = UIMarkupTextPrintFormatter(markupText: HTMLContent) let printPageRenderer = CustomPrintPageRenderer() printPageRenderer.addPrintFormatter(printFormatter, startingAtPageAt: 0) let pdfData = drawPDFUsingPrintPageRenderer(printPageRenderer: printPageRenderer) pdfFilename = "\(AppDelegate.getAppDelegate().getDocDir())/FinalPdf.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 }
我已经关注了app coda教程.对于多个页面,它表示包括PDF页面创建的开头和打印页面渲染器绘制到循环中.请帮我创建多页PDF.
使用下面的代码代替您当前的drawPDFUsingPrintPageRenderer函数:
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 }
中提琴,多页.