7

I created two different pdf files in two different views using following code:

private func toPDF(views: [UIView]) -> NSData? {

    if views.isEmpty {return nil}

    let pdfData = NSMutableData()
    UIGraphicsBeginPDFContextToData(pdfData, CGRect(x: 0, y: 0, width: 1024, height: 1448), nil)
    let context = UIGraphicsGetCurrentContext()

    for view in views {
        UIGraphicsBeginPDFPage()
        view.layer.renderInContext(context!)
    }

    UIGraphicsEndPDFContext()

    return pdfData
}

In the final view I call both files using:

let firstPDF = NSUserDefaults.standardUserDefaults().dataForKey("PDFone")
let secondPDF = NSUserDefaults.standardUserDefaults().dataForKey("PDFtwo")

My question is: Can anyone suggest a function which append the second file to the first one? (Both are in NSData Format)

Larme
  • 24,190
  • 6
  • 51
  • 81
Ali
  • 83
  • 1
  • 6

3 Answers3

12

Swift 4:

func merge(pdfs:Data...) -> Data
{
    let out = NSMutableData()
    UIGraphicsBeginPDFContextToData(out, .zero, nil)

    guard let context = UIGraphicsGetCurrentContext() else {
        return out as Data
    }

    for pdf in pdfs {
        guard let dataProvider = CGDataProvider(data: pdf as CFData), let document = CGPDFDocument(dataProvider) else { continue }

        for pageNumber in 1...document.numberOfPages {
            guard let page = document.page(at: pageNumber) else { continue }
            var mediaBox = page.getBoxRect(.mediaBox)
            context.beginPage(mediaBox: &mediaBox)
            context.drawPDFPage(page)
            context.endPage()
        }
    }

    context.closePDF()
    UIGraphicsEndPDFContext()

    return out as Data
}
Andreas Ley
  • 9,109
  • 1
  • 47
  • 57
  • This will work, but it removes any annotations you added, like links. I found the easy way was is to create multiple PDFDocuments from your data, then for each one grab its PDFPage. From those pages you then can insert each of those pages into a new PDFDocument – DogCoffee Dec 04 '19 at 21:17
  • @DogCoffee if you found a solution, I'd be interested. Thanks... How do you convert PDF into Data? – FrugalResolution Oct 04 '20 at 02:09
  • https://stackoverflow.com/a/64190865/12035498 DogCoffee's answer in code: Merging is working for me in swift5 – FrugalResolution Oct 04 '20 at 03:21
9

This can be done quite easily with PDFKit and its PDFDocument.

I'm using this extension:

    import PDFKit
    
    extension PDFDocument {
    
        func addPages(from document: PDFDocument) {
            let pageCountAddition = document.pageCount
    
            for pageIndex in 0..<pageCountAddition {
                guard let addPage = document.page(at: pageIndex) else {
                    break
                }
    
                self.insert(addPage, at: self.pageCount) // unfortunately this is very very confusing. The index is the page *after* the insertion. Every normal programmer would assume insert at self.pageCount-1
            }
        }
    
    }
JuliusBahr
  • 91
  • 1
  • 3
  • Remember to save the edited PDF, or else you won't see the pages added to the file. For reference, this solution https://stackoverflow.com/a/53857161/5691990 – hermannb May 13 '23 at 13:15
0

Swift 5:

Merge pdfs like this to keep links, etc...

See answer here

FrugalResolution
  • 568
  • 4
  • 18