title: How to create a PDF in Swift with Cocoa (Mac)
body: <p>Xcode 7.3.2, Swift 2, Cocoa (Mac).</p>
  
  <p>My app involves the user entering in some text, which can be exported to a PDF. </p>
  
  <p>In the iOS version of my app, I can create the PDF relatively easily with the <code>CoreText</code> framework:</p>
  
  <pre><code>let html = "&lt;font face=\'Futura\' color=\"SlateGray\"&gt;&lt;h2&gt;\(title)&lt;/h2&gt;&lt;/font&gt;&lt;font face=\"Avenir\" color=\"SlateGray\"&gt;&lt;h4&gt;\(string)&lt;/h4&gt;&lt;/font&gt;"
  
      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)
  </code></pre>
  
  <p>However, <code>UIMarkupTextPrintFormatter</code>, <code>UIPrintPageRenderer</code>, <code>UIGraphicsBeginPDFContextToData</code>, and <code>UIGraphicsEndPDFContext</code> all do not exist on OS X. How can I do the exact same thing as I am doing with this iOS code (create a basic PDF from some HTML and write it to a certain file path as a paginated PDF) with Mac and Cocoa?</p>
  
  <p><strong>EDIT:</strong> The answer to this question is here: <a href="https://stackoverflow.com/questions/39135233/create-a-paginated-pdf-mac-os-x/39479101#39479101">Create a paginated PDF—Mac OS X</a>.</p>