Create a pdf from a view in iOS

Is there a simple way using Swift to create a pdf from a view in iOS and email it. The view only contains Label items and was formatted in a storyboard.

See the following APIs:


  • UIGraphicsBeginPDFContextToData (to start a PDF context that will capture all drawing to an NSMutableData instance
  • UIGraphicsBeginPDFPage (to start a PDF page)
  • Call 'draw(in:)' on your view's layer using the currently set graphics context (which will be the PDF context)
  • UIGraphicsEndPDFContext (to end drawing)
  • UIActivityViewController (where you can pass it the PDF data you created to be shared; e.g. sent via email, printed, etc.)

Are you expecting to email silently/without user interaction?

Is there not a simple API that translates a view into a pdf?

There's one line of code to take a view's layer and draw it into a PDF context. The total lines of code for what you want to do is only around six.

Create a pdf from a view in iOS
 
 
Q