Maurício Linhares
Maurício Linhares

Reputation: 40333

How do I generate PDFs from images in Objective-C? (on Mac OS)

I'm trying to build the code that will generate a single PDF from a bunch of images in Objective-C. Most of examples I've seen so far don't really allow me to build the PDF, but only generating Cocoa views and turning them into PDFs.

What I'm looking for is some toolkit (like Java's iText) that would allow me to generate a PDF from a bunch of images (with each image being a page in the PDF document).

Any hints?

Upvotes: 1

Views: 5096

Answers (2)

VBK
VBK

Reputation: 148

It's simpler with PDFKit.

PDFDocument *pdf = [[PDFDocument alloc] init];
for (NSString *fileName in fileNames)
{
    NSImage *image = [[NSImage alloc] initWithContentsOfFile:fileName]; 
    PDFPage *page = [[PDFPage alloc] initWithImage:image];
    [pdf insertPage:page atIndex: [pdf pageCount]];
    [image release];
    [page release];
}

[pdf writeToFile: @"output.pdf"];

Upvotes: 5

Vladimir
Vladimir

Reputation: 170859

See Creating a PDF File section in Quartz 2D programming guide.

There's a sample code to create a pdf document. All you'll need to do there is implement your custom myDrawContent function, where you can draw your images to pdf context same way as to any other CGContextRef.

Upvotes: 2

Related Questions