Sample code for 30+ languages & platforms
Swift

PDF File Encoding to Base64

See more Base64 Examples

Demonstrates how to encode a PDF file to base64, and then decode.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let pdfData = CkoBinData()!

    success = pdfData.loadFile(path: "qa_data/helloWorld.pdf")
    if success != true {
        print("failed to load PDF file.")
        return
    }

    // Encode the PDF to base64
    // Note: to produce base64 on multiple lines (as it would appear in the MIME of an email),
    // pass the string "base64_mime" instead of "base64".
    var b64: String? = pdfData.getEncoded(encoding: "base64")
    print("\(b64!)")

    // Decode from base64 PDF.
    let pdfData2 = CkoBinData()!
    pdfData2.appendEncoded(encData: b64, encoding: "base64")
    success = pdfData2.writeFile(path: "qa_output/helloWorld2.pdf")
    if success != true {
        print("failed to write PDF file.")
        return
    }


}