Skip to content

Swift-GemPDFKit is a lightweight (and by no means complete) library for parsing, writing, and adding attachments to PDFs. The PDF-parsing and -tokenization depends heavily on PointFree's swift-parsing library (https://github.com/pointfreeco/swift-parsing).

License

Notifications You must be signed in to change notification settings

gematik/swift-gemPDFKit

Repository files navigation

logo

Swift-GemPDFKit

About The Project

Swift-GemPDFKit is a (by no means complete) library for parsing, writing, and adding attachments to PDFs. The PDF-parsing and -tokenization depends heavily on Pointfree's swift-parsing library.

Release Notes

See ReleaseNotes for all information regarding the (newest) releases.

Getting Started

You can parse a PDF file and then add attachments to it:

// Parse the original PDF data and data to be attached
let originalPdfData = try testResource(name: "simple_pdf", extension: "pdf")
let toBeAttachedData = try testResource(name: "somedata", extension: "")

// Parse the PDF data
let originalPdfDataString = String(data: originalPdfData, encoding: .ascii)!.utf8
let parsedPdfDocument = try PDFDocument.PDFDocumentParserPrinter().parse(originalPdfDataString)

// Render the attachment data before appending
let renderedAttachmentData = try parsedPdfDocument.append(
    attachment: .init(filename: "attachmentFilename🧸", content: toBeAttachedData),
    startObj:  originalPdfData.count
)

// Add the rendered attachment data to the original PDF data
let pdfWithAttachmentData = originalPdfData + renderedAttachmentData

// Write the data to your system
let outputPath = outputResourceURL(name: "output", extension: "pdf")
try pdfWithAttachmentData.write(to: outputPath)

You can also extract attachments from a PDF:

let pdfWithAttachmentDataString = String(data: pdfWithAttachmentData, encoding: .ascii)!.utf8
let parsedPdfWithAttachment = try PDFDocument.PDFDocumentParserPrinter().parse(pdfWithAttachmentDataString)
let attachments = try parsedPdfWithAttachment.allAttachments()

expect(attachments.count).to(equal(1)) // ✅
expect(attachments.first?.content).to(equal(toBeAttachedData)) // ✅

Documentation

License

Copyright 2023 gematik GmbH

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.

See the LICENSE for the specific language governing permissions and limitations under the License.

Unless required by applicable law the software is provided "as is" without warranty of any kind, either express or implied, including, but not limited to, the warranties of fitness for a particular purpose, merchantability, and/or non-infringement. The authors or copyright holders shall not be liable in any manner whatsoever for any damages or other claims arising from, out of or in connection with the software or the use or other dealings with the software, whether in an action of contract, tort, or otherwise.

The software is the result of research and development activities, therefore not necessarily quality assured and without the character of a liable product. For this reason, gematik does not provide any support or other user assistance (unless otherwise stated in individual cases and without justification of a legal obligation). Furthermore, there is no claim to further development and adaptation of the results to a more current state of the art.

Gematik may remove published results temporarily or permanently from the place of publication at any time without prior notice or justification.

About

Swift-GemPDFKit is a lightweight (and by no means complete) library for parsing, writing, and adding attachments to PDFs. The PDF-parsing and -tokenization depends heavily on PointFree's swift-parsing library (https://github.com/pointfreeco/swift-parsing).

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published