Jaco Griesel
Jaco Griesel

Reputation: 375

Create Multi-Page PDF from other PDFs

I've previously posted a related question on SO, but to no avail. I then changed my approach and created PDFs which all have the exact same dimensions, etc. All these PDFs also consists of one page only.

Now I would like to combine these single page PDFs into one multi-page PDF. From here I think I've figured what the steps are in creating the multi-page PDF.

After running the code below, a PDF with the expected file name is created, but the PDF consists of only one page, and it is entirely blank.

I'm at wits' end here... Please tell me what I'm doing wrong! Yes, I believe some sort of loop will work here but, to be honest, LOOPS have always gotten the better of me.... :(

Any help will be greatly appreciated!

Oh, and I can barely Swift - please do not throw any Obj-C at me! ;)

Here's my code:

func CreateCombinedPDF() {

    let pdf01 = String("\(newDetailsLogIdentifier)_PAGE01.pdf")
    let pdf02 = String("\(newDetailsLogIdentifier)_PAGE02.pdf")

    //STEPS IN CREATING A COMBINED PDF

    // 1. CGPDFDocumentCreateWithURL
    // 2. CGContextBeginPage
    // 3. CGPDFDocumentGetPage
    // 4. CGPDFContextCreateWithURL
    // 5. CGContextDrawPDFPage
    // 6. CGContextEndPage
    // 7. CGPDFContextClose

    var mediaBox:CGRect = CGRectMake(0, 0, 820, 1170)
    let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    let combinedDocumentFileName = documentsURL.URLByAppendingPathComponent("\(newDetailsLogIdentifier)_COMBINED.pdf")
    let fullPathCombinedDocument = combinedDocumentFileName.path!
    let myCombinedDocumentURL = NSURL(fileURLWithPath: fullPathCombinedDocument)
    let myContextCombinedDocument = CGPDFContextCreateWithURL(myCombinedDocumentURL, &mediaBox, nil)

    let fileNamePDF01 = documentsURL.URLByAppendingPathComponent(pdf01)
    let fullPathPDF01 = fileNamePDF01.path!
    let urlPDF01 = NSURL(fileURLWithPath: fullPathPDF01)
    let myContextPDF01 = CGPDFContextCreateWithURL(urlPDF01, &mediaBox, nil)
    CGPDFContextBeginPage(myContextPDF01, nil)
    //Here's my problem - I think...
    CGContextDrawPDFPage(myContextPDF01, nil)
    CGPDFContextEndPage(myContextPDF01)

    let fileNamePDF02 = documentsURL.URLByAppendingPathComponent(pdf02)
    let fullPathPDF02 = fileNamePDF02.path!
    let urlPDF02 = NSURL(fileURLWithPath: fullPathPDF02)
    let myContextPDF02 = CGPDFContextCreateWithURL(urlPDF02, &mediaBox, nil)
    CGPDFContextBeginPage(myContextPDF02, nil)
    //Here's my problem - I think...
    CGContextDrawPDFPage(myContextPDF02, nil)
    CGPDFContextEndPage(myContextPDF02)

    CGPDFContextClose(myContextCombinedDocument)

}

Upvotes: 0

Views: 1387

Answers (2)

Quan Vu
Quan Vu

Reputation: 1

Swift code above wasn't working for me, so I converted to Objective-C for those interested.

+(void) CreateCombinedPDF {
    NSString *newDetailsLogIdentifier = @"filename";

    // Set all constants and variables needed
    CGRect mediaBox = CGRectMake(0, 0, 820, 1170);
    NSFileManager *fm = [NSFileManager defaultManager];
    NSURL *documentsURL = [fm URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask][0];
    NSURL *combinedDocumentFileName = [documentsURL URLByAppendingPathComponent:[NSString stringWithFormat:@"%@_COMBINED.pdf", newDetailsLogIdentifier]];
    NSString *fullPathCombinedDocument = combinedDocumentFileName.path;
    NSURL *myCombinedDocumentURL = [NSURL fileURLWithPath:fullPathCombinedDocument];

    NSString *pdf01 = [NSString stringWithFormat:@"%@_PAGE01.pdf", newDetailsLogIdentifier];
    NSURL *fileNamePdf01 = [documentsURL URLByAppendingPathComponent:pdf01];
    NSString *fullPathPdf01 = fileNamePdf01.path;
    NSURL *urlPDF01 = [NSURL fileURLWithPath:fullPathPdf01];
    CFURLRef cfurl = CFBridgingRetain(urlPDF01);
    CGPDFDocumentRef contextPDF01 = CGPDFDocumentCreateWithURL(cfurl);
    CGPDFPageRef pdf01Page = CGPDFDocumentGetPage(contextPDF01, 1);

    NSString *pdf02 = [NSString stringWithFormat:@"%@_PAGE02.pdf", newDetailsLogIdentifier];
    NSURL *fileNamePdf02 = [documentsURL URLByAppendingPathComponent:pdf02];
    NSString *fullPathPdf02 = fileNamePdf02.path;
    NSURL *urlPDF02 = [NSURL fileURLWithPath:fullPathPdf02];
    CFURLRef cfurl2 = CFBridgingRetain(urlPDF02);
    CGPDFDocumentRef contextPDF02 = CGPDFDocumentCreateWithURL(cfurl2);
    CGPDFPageRef pdf02Page = CGPDFDocumentGetPage(contextPDF02, 1);

    // 1. Create the PDF context that will become the new PDF file
    CGContextRef myContextCombinedDocument = CGPDFContextCreateWithURL(CFBridgingRetain(myCombinedDocumentURL), &mediaBox, nil);

    // 2. Insert pages

    // Draw PAGE01.pdf
    CGPDFContextBeginPage(myContextCombinedDocument, nil);
    CGContextDrawPDFPage(myContextCombinedDocument, pdf01Page);
    CGPDFContextEndPage(myContextCombinedDocument);

    // Draw PAGE02.pdf
    CGPDFContextBeginPage(myContextCombinedDocument, nil);
    CGContextDrawPDFPage(myContextCombinedDocument, pdf02Page);
    CGPDFContextEndPage(myContextCombinedDocument);

    // 3. All pages inserted.  Now close and save the new document.
    CGPDFContextClose(myContextCombinedDocument);
}

Upvotes: 0

Jaco Griesel
Jaco Griesel

Reputation: 375

As mentioned in the comments above, the code I've posted was crap. Now I've got it sorted out - the multi-page PDF gets created.

I still have to work on that loop I've mentioned, but for now this works for me (without a loop):

func CreateCombinedPDF() {

    //Set all constants and variables needed
    var mediaBox:CGRect = CGRectMake(0, 0, 820, 1170)
    let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    let combinedDocumentFileName = documentsURL.URLByAppendingPathComponent("\(newDetailsLogIdentifier)_COMBINED.pdf")
    let fullPathCombinedDocument = combinedDocumentFileName.path!
    let myCombinedDocumentURL = NSURL(fileURLWithPath: fullPathCombinedDocument)

    let pdf01 = String("\(newDetailsLogIdentifier)_PAGE01.pdf")
    let fileNamePDF01 = documentsURL.URLByAppendingPathComponent(pdf01)
    let fullPathPDF01 = fileNamePDF01.path!
    let urlPDF01 = NSURL(fileURLWithPath: fullPathPDF01)
    let contextPDF01 = CGPDFDocumentCreateWithURL(urlPDF01)
    let pdf01Page = CGPDFDocumentGetPage(contextPDF01,1)

    let pdf02 = String("\(newDetailsLogIdentifier)_PAGE02.pdf")
    let fileNamePDF02 = documentsURL.URLByAppendingPathComponent(pdf02)
    let fullPathPDF02 = fileNamePDF02.path!
    let urlPDF02 = NSURL(fileURLWithPath: fullPathPDF02)
    let contextPDF02 = CGPDFDocumentCreateWithURL(urlPDF02)
    let pdf02Page = CGPDFDocumentGetPage(contextPDF02,1)

    // 1. Create the PDF context that will become the new PDF file
    let myContextCombinedDocument = CGPDFContextCreateWithURL(myCombinedDocumentURL, &mediaBox, nil)

    // 2.  Insert pages

    //Draw PAGE01.pdf
    CGPDFContextBeginPage(myContextCombinedDocument, nil);
    CGContextDrawPDFPage(myContextCombinedDocument, pdf01Page)
    CGPDFContextEndPage(myContextCombinedDocument)

    //Draw PAGE02.pdf
    CGPDFContextBeginPage(myContextCombinedDocument, nil);
    CGContextDrawPDFPage(myContextCombinedDocument, pdf02Page)
    CGPDFContextEndPage(myContextCombinedDocument)

    // 3.  All pages inserted.  Now close and save the new document.
    CGPDFContextClose(myContextCombinedDocument)
}

It might not be elegant, but it sure as hell works!

Kudos to the information I found here!

Upvotes: 1

Related Questions