Xcode 7.3.2, Swift 2, Cocoa (Mac).
My application includes a user who enters text that can be exported to a PDF file.
In the version of my iOS application, I can easily create a PDF using the CoreText framework:
let html = "<font face=\'Futura\' color=\"SlateGray\"><h2>\(title)</h2></font><font face=\"Avenir\" color=\"SlateGray\"><h4>\(string)</h4></font>" let fmt = UIMarkupTextPrintFormatter(markupText: html) // 2. Assign print formatter to UIPrintPageRenderer let render = UIPrintPageRenderer() render.addPrintFormatter(fmt, startingAtPageAt: 0) // 3. Assign paperRect and printableRect let page = CGRect(x: 10, y: 10, width: 595.2, height: 841.8) // A4, 72 dpi, margin of 10 from top and left. 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 pdfData = NSMutableData() UIGraphicsBeginPDFContextToData(pdfData, 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 path = "\(NSTemporaryDirectory())\(title).pdf" pdfData.write(toFile: path, atomically: true)
However, UIMarkupTextPrintFormatter , UIPrintPageRenderer , UIGraphicsBeginPDFContextToData and UIGraphicsEndPDFContext all do not exist on OS X. How can I do the same thing as me with this iOS code (create a basic PDF file from some HTML and write it to a specific file path PDF) with Mac and Cocoa?
EDIT: The answer to this question is here: Create paginated PDF-Mac OS X.
source share