0

When generating PDFs on iOS, it seems that the format version of the resulting file always defaults to version 1.3 of the PDF specification. Even when opening an existing PDF file with format version 1.7, after the file is written back to disk the format version is changed to 1.3. This behavior is observable with each of Apple's PDF APIs as shown by the following example:

let documentDirectoryUrl = FileManager.default.urls(for: .documentDirectory,
                                                    in: .userDomainMask).first

let pdfVersion: ((URL) -> (String?)) = { url in
    if let pdf = PDFDocument(url: url) {
        return "\(pdf.majorVersion).\(pdf.minorVersion)"
    }
    return nil
}

if let url = documentDirectoryUrl?.appendingPathComponent("pdfKit").appendingPathExtension("pdf") {
    let pdf = PDFDocument()
    pdf.write(to: url)
    print("PDFKit \(pdfVersion(url) ?? "")")
}

if let url = documentDirectoryUrl?.appendingPathComponent("uiKit").appendingPathExtension("pdf"),
   let context = CGContext(url as CFURL, mediaBox: nil, nil) {
    context.closePDF()
    print("UIKit \(pdfVersion(url) ?? "")")
}

if let url = documentDirectoryUrl?.appendingPathComponent("coreGraphics").appendingPathExtension("pdf") {
    UIGraphicsBeginPDFContextToFile(url.path, .zero, nil)
    UIGraphicsEndPDFContext()
    print("CoreGraphics \(pdfVersion(url) ?? "")")
}

What's interesting is that there are several references in Apple's documentation and header files that mention version 1.7 of the PDF specification, for example: CGPDFContext.addDocumentMetadata(_:)

How can I specify the format version when writing a PDF file on iOS? Is this even possible?

Thanks!

5
  • hmmm the idea is to designate the lowest compatible number so 1.0 is better than 2,0 so 1.4 needs transparency support but 1.3 does not etc.
    – K J
    Jul 14 at 18:28
  • For my use case I’m interested in preserving/editing XMP metadata associated with the document, so I think that requires at least 1.4, right?
    – ryanecrist
    Jul 14 at 23:03
  • I'm no expert on differences but since Adobe did not cast in stone some xmp until they published 1.7 as a common level for pdf/A-2 and up then 1.7 is a safe bet but 1.4+ viewers should work with the then known basics of PDF/A-1 ISO 19005-1. It is based on PDF 1.4, the file format of Acrobat 5, XMP was introduced with Acrobat 5 and PDF 1.4 in 2001. Ideally you should compare requirements against both levels
    – K J
    Jul 14 at 23:12
  • Got it, thanks - yeah it's curious that even when I start with a 1.7 PDF with existing XMP, after writing it back to disk using the iOS SDKs all XMP is removed.
    – ryanecrist
    Jul 14 at 23:24
  • that's the point about using 1.3 for viewing as xmp is not needed except if you need PDFA compliance in a writer/reader, 'orses for courses
    – K J
    Jul 14 at 23:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.