PDF printing from app data

Hi all!

I’m trying to figure out the best way to generate multipage pdf reports with headers and footers from data that I have in my app.

Since I’m a graphically oriented person I wonder if there is a way to do the pdf layout in storyboard or as xib-files? If not what is the best way to do it in code?

I found this tutorial here at RW:
https://videos.raywenderlich.com/courses/27-intermediate-core-graphics/lessons/6

But it seems to me that this is somewhat dated since Apple released new UIKit methods in iOS10, half a year after that RW video tutorial was released:
https://developer.apple.com/documentation/uikit/uigraphicspdfrenderer#topics

Right now I use a hack that I made with PDFKit in iOS11 where I just draw PDFAnnotation text on an empty pdf. But this can’t obviously not be the right solution since I cannot handle wrapping of long texts nor page brakes.

So please can anyone tell me what the best practices are when it comes to print a PDF file from data within an iOS application?

BR /Diez

@caroline Can you please help with this when you get a chance? Thank you - much appreciated! :]

@nobach Please check out the PDFKit chapter in our iOS 11 by Tutorials book when you get a chance:

https://store.raywenderlich.com/products/ios-11-by-tutorials

I hope it helps. Thank you!

I actually bought that book just because of this problem. It explains well how to show PDF:s with PDFKit in your app but unfortunately it doesn’t tell you anything about how to print (with print I mean generate pdf from app data) them thou… :slight_smile:

@macandyp Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hey @nobach,

I’m not entirely sure this is possible. From the documentation, it looks as though it requires that you override draw(with:).

Adding Custom Graphics to a PDF documentation

Edit: just saw your other comment. Sorry it wasn’t totally clear, but you are somewhat generating PDF content in the sample. You can actually generate a PDFPage and PDFDocument with annotations and then save that data, thus generating a brand new PDF. It does require you to handle what your document should look like in code.

This topic was automatically closed after 166 days. New replies are no longer allowed.