我想在我的iOS应用中创建一个PDF / A文件但不知道从哪里开始。
我的实际代码(生成一个简单的PDF)是:
_ = UIGraphicsBeginPDFContextToFile(temporaryPdfFilePath, paperSize, nil)
UIGraphicsBeginPDFPageWithInfo(paperSize, nil)
let currentContext: CGContextRef = UIGraphicsGetCurrentContext()!
... ...绘制
UIGraphicsEndPDFContext()
是否有任何API或其他内容可供使用?
答案 0 :(得分:6)
准备html文件,然后转换为pdf。 Swift 3.0版:
func drawPDFUsingPrintPageRenderer(printPageRenderer: UIPrintPageRenderer) -> NSData! {
let data = NSMutableData()
UIGraphicsBeginPDFContextToData(data, CGRect.zero, nil)
UIGraphicsBeginPDFPage()
printPageRenderer.drawPage(at: 0, in: UIGraphicsGetPDFContextBounds())
UIGraphicsEndPDFContext()
return data
}
func exportHTMLContentToPDF(HTMLContent: String) {
let printPageRenderer = CustomPrintPageRenderer()
let printFormatter = UIMarkupTextPrintFormatter(markupText: HTMLContent)
printPageRenderer.addPrintFormatter(printFormatter, startingAtPageAt: 0)
let pdfData = drawPDFUsingPrintPageRenderer(printPageRenderer: printPageRenderer)
let pdfFilename = getDocumentsDirectory().appendingPathComponent("nameOfPDFfile.pdf")
pdfData?.write(to: pdfFilename, atomically: true)
}
}
答案 1 :(得分:1)
答案 2 :(得分:0)
//
// ViewController.swift
// Ganrate_pdf
//
// Created by Tops on 3/23/17.
// Copyright © 2017 tops. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var lbl_name: UILabel!
@IBOutlet weak var web_view: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// create html code
let html = "<b>Welcome <i>World!</i></b> <p><h1><font color='red'>This is PDF file In swift 3.0</font></h1></p>"
let frmt = UIMarkupTextPrintFormatter(markupText: html)
// set print format
let render = UIPrintPageRenderer()
render.addPrintFormatter(frmt, startingAtPageAt: 0)
// Create Paper Size for print
let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8)
let printable = page.insetBy(dx: 0, dy: 0)
render.setValue(NSValue(cgRect: page), forKey: "paperRect")
render.setValue(NSValue(cgRect: printable), forKey: "printableRect")
// 4. Create PDF context and draw
let filedata = NSMutableData()
UIGraphicsBeginPDFContextToData(filedata, CGRect.zero, nil)
for i in 1...render.numberOfPages {
UIGraphicsBeginPDFPage();
let bounds = UIGraphicsGetPDFContextBounds()
render.drawPage(at: i - 1, in: bounds)
}
UIGraphicsEndPDFContext();
// 5. Save PDF file you can also save this file by using another button
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
filedata.write(toFile: "\(documentsPath)/myfile.pdf", atomically: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Button on view for show the file in webview
@IBAction func Ganratepdf(_ sender: UIButton)
{
let file="myfile"
self.ganarate_pdf(filename: file)
}
func ganarate_pdf(filename:String)
{
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let filePath = "\(documentsPath)/\(filename).pdf"
let url = URL(fileURLWithPath: filePath)
let urlRequest = URLRequest(url: url)
web_view.loadRequest(urlRequest)
}
}
答案 3 :(得分:0)
我写了一个使用HTML模板创建PDF的窗格:
https://github.com/NGromann/SwiftHtmlPdf
如果usage guide没有帮助,您也可以看看example project。