Generating PDF SwiftUI

Hi,

I am in need of a solution that takes data from SwiftData classes and generates a PDF with the given data. Any guidance would be greatly appreciated.

Start here: drawingwithquartz2d

Hi,

Thank you for your response however I'm not fully following the documentation you provided. I have a few questions:

Listing 13-2 describes drawing a PDF page, but the function is declared with "void". What language is that?

I searched CGContext and found many functions but I'm not sure how to use them. Would I have to create a CGContext object and call the functions on that? Is there a function for plain text?

Listing 13-4 is a larger example, but I don't see any code that specifies the actual content being added to the PDF.

the function is declared with "void". What language is that?

C

But… that part of the document is about reading a PDF document, not creating one.

I think you want this part for creating PDF:

https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/doc/uid/TP30001066-CH203-TPXREF118

Swift docs for CGContext:

https://developer.apple.com/documentation/coregraphics/cgcontext

There may be other, non-Apple, ways to do this.

Is the documentation you provided also in C?

The first link, yes. Second link, no.

I think that the C docs, in particular the introductory sections, are probably worth looking at because they were written by actual human technical writers before Apple fired them all. The modern docs are automatically generated from the code and much harder to understand, especially in terms of understanding the basic concepts. But if you get totally bogged down by not understanding the C, then just use the Swift docs at the second link.

is there a way to include a C file in a SwiftUI project?

Yes, but you probably don't need to do that.

What are values, keys, and dictionaries?

In Swift: https://developer.apple.com/documentation/swift/dictionary

In the C code you're looking at, it's doing essentially the same thing but in a much more verbose fashion because these things are not built in to the language, but requite lots of library function calls to use.

I see how to draw shapes but how do you include text?

https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_text/dq_text.html#//apple_ref/doc/uid/TP30001066-CH213-TPXREF101

I believe that you will need to use Core Text.

Note: You may find 3rd-party libraries that are easier to use than all this Apple functionality. I'd say that using Quartz / Core Graphics / Core Text to create PDFs makes sense if you are already using those to create on-screen graphics within your app. If you aren't, do at least investigate what other options exist. I just googled "github swift PDF" and there are a few things. Of course you're not likely to get much feedback about those on this forum.

I found this: https://forums.developer.apple.com/forums/thread/671947

It explains a lot. I just have a few follow up questions:

I'm getting errors with these two lines:

 context.setFillColor(NSColor.red.cgColor)
        context.addPath(shape.path(in: CGRect(origin: .zero, size: size)))

Additionally, I see a lot of functions on the CGContext documentation page for text under the heading "Drawing Text" that does things such as setting the font, but I don't see a function that does the drawing.

Also, there is code here that downloads a file of type FileDocument. Is there a way to convert the Data type (from the first link) generated with the code provided above to a FileDocument type? Could I also use a .fileExporter call to save the Data type or, is there a better way to save the file (for iOS)?

NSColor.red.cgColor

NSColor is for macOS. Use UIColor on iOS.

I see a lot of functions on the CGContext documentation page for text under the heading "Drawing Text" that does things such as setting the font, but I don't see a function that does the drawing.

I believe you need to use Core Text instead of all that. The CGContext text drawing functions do exist, but you don’t immediately find them in the documentation because they are deprecated. Example: https://developer.apple.com/documentation/coregraphics/cgcontext/1586507-showtext

I don’t know about the fileExporter thing.

Thank you for all the help thus far. Can Core Text be used in conjunction with CGContext? Is PDFKit compatible with iOS? If so, would that be better to use?

Can Core Text be used in conjunction with CGContext?

Yes. See e.g. https://developer.apple.com/documentation/coretext/1509589-ctframedraw for how to draw a Core Text “frame” into a Core Graphics CGContext.

Is PDFKit compatible with iOS?

If you mean this:

https://developer.apple.com/documentation/pdfkit

then I believe it is only useful for displaying PDF, not creating it.

Here's what I've put together so far:

let context = CGContext()
let title = CTLineCreateWithAttributedString(CFAttributedString("Title"))

One thing I don't understand is CGContext has many different of options as for parameters; some has width/height, some has other parameters. Which initializer would be ideal to use and what is the difference between setting the CTFrame (which I also don't know how to initialize) and the width/height of CGContext?

I am also unsure as how to pass a string to CTLineCreateWithAttributedString. I tried this:

let title = CTLineCreateWithAttributedString(CFAttributedString("Title"))

Also, how do I map something like the CTLine to the context?

I’m afraid you’ve more-or-less reached the end of my knowledge of this stuff.

how do I map something like the CTLine to the context?

Using this:

https://developer.apple.com/documentation/coretext/1511145-ctlinedraw

Generating PDF SwiftUI
 
 
Q