StanfordSpezi / SpeziOnboarding

Spezi Onboarding module to inform a user or retrieve consent for a study participation
https://swiftpackageindex.com/StanfordSpezi/SpeziOnboarding/documentation/
MIT License
11 stars 5 forks source link

Added algorithm for pagination to PDF export, allowing to export consent forms with more than 1 page (#49) #52

Open RealLast opened 2 weeks ago

RealLast commented 2 weeks ago

Fixing exported PDF being truncated if text is larger than 1 page(#49)

:recycle: Current situation & Problem

Closes #49 If the text in the consent document is longer than 1 page, then the text was truncated during the PDF export of a consent document. The ImageRenderer cannot automatically split the rendered text across multiple pages.

:gear: Release Notes

:books: Documentation

*Documented changes in the appropriate Markdown files.

:white_check_mark: Testing

:pencil: Code of Conduct & Contributing Guidelines

By submitting creating this pull request, you agree to follow our Code of Conduct and Contributing Guidelines:

RealLast commented 2 days ago

Thank you very much @PSchmiedmayer for having a look at this and providing detailed feedback!

I found TPPDF to be highly interesting and it seems to be in a good state. So I had a closer look at it and drafted a proof-of-concept for using TPPDF to do the PDF generation in Spezi. You can check it here. I made it backward compatible, the export still returns a PDFKit.PDFDocument (as opposed to TPPDF.PDFDocument). I simply use TPDDF to generate the PDF, and then convert it to PDFKit.PDFDocument.

That being said, TPDDF is a breeze to use for generating a (multi-page) PDF. It only takes 3 lines to add the title, text, and signature:

let document = TPPDF.PDFDocument(format: .usLetter)

let header = await headerToImage()
let signature = await signatureToImage()

document.add(image: PDFImage(image: header))
document.add(attributedText: NSAttributedString(markdownString))
document.add(image: PDFImage(image: signature))

let generator = PDFGenerator(document: document)

You can find an example of a two-page consent document PDF generated with TPPDF from Spezi below. Currently, I still render the title and signature footer as Images, but we can also change it to completely use the text rendering of TPPDF. I think the solution is very promising. The only problem is that I could not get the markdown attribution to work. While TPPDF supports AttributedStrings, somehow when generating an AttributedString from markdown it just renders the plain text without any attribution. I can look into this further, if we think that this is our preferred solution.

Signed Consent Form98.pdf