I'd like to create a PDF/A file in my iOS app but don't really know where to start.
My actual code (which generates a simple PDF) is:
_ = UIGraphicsBeginPDFContextToFile(temporaryPdfFilePath, paperSize, nil)
UIGraphicsBeginPDFPageWithInfo(paperSize, nil)
let currentContext: CGContextRef = UIGraphicsGetCurrentContext()!
...drawing...
UIGraphicsEndPDFContext()
Is there any API or something else to follow?
Prepare html file and then convert to pdf. Swift 3.0 version:
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)
}
}
Great tutorial: http://www.appcoda.com/pdf-generation-ios/
There are quite a few pods to create PDFS easily. I just used SimplePDF and it was a breeze.
SimplePDF: https://github.com/nRewik/SimplePDF
I hope this helps someone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With