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

Export Signed Consent Form #7

Closed vishnuravi closed 8 months ago

vishnuravi commented 1 year ago

Problem

Currently, although the user can view and sign a consent document, there is no way to save or export this document.

Solution

Add functionality to save and/or export a signed consent document, ideally in the form of a PDF.

Code of Conduct

PSchmiedmayer commented 1 year ago

@vishnuravi Thank you for sharing this feature request!

PSchmiedmayer commented 1 year ago

Here is some additional context for anyone who would like to implement this feature, here are some hints on how

1. Obtain the signature:

The consent view in this module already exposes the PKDrawing of the signature view as a private property that can be used to obtain the image representation of the drawing and then inserted in a PDF: https://github.com/StanfordSpezi/SpeziOnboarding/blob/main/Sources/SpeziOnboarding/ConsentView.swift#L52.

The size of the view can be retrieved using the following view preference: https://github.com/StanfordSpezi/SpeziViews/blob/main/Sources/SpeziViews/Views/Drawing/CanvasView.swift#L121.

2. Create a PDF Representation of the consent view:

We can imagine multiple different mechanisms to do so

  1. Obtain a PDF on the creation of the view from the developer with a specification where the signature and name should be inserted
  2. Generate a PDF from the markdown or HTML view in the view:

The second possibility might be a bit more challenging as we need to create a PDF from a SwiftUI view of undetermined size, but several new APIs might be able to help us with that, e.g.,: https://www.hackingwithswift.com/quick-start/swiftui/how-to-render-a-swiftui-view-to-a-pdf

Please use the comments under this issue to discuss the best approach to addressing this issue, and feel free to share some first drafts of a PR for discussion if you want to address this issue.

PSchmiedmayer commented 9 months ago

@vishnuravi @philippzagar I am not sure what the current state with this issue is. Feel free to provide an update how things are going here?

@vishnuravi I would like to clean up the following branch in the Spezi Module to ensure that we moved everything to different modules and prioritize important issues: https://github.com/StanfordSpezi/Spezi/tree/create-pdf. Can you move that over here for a draft PR and/or something that you or @philippzagar can work on?

In addition to that, I would suggest that we drop any ideas of a closure, based approach but rather focus on adding a Standard constraint that must be present if the Consent View is used to export a PDF.

One additional link that I would like to share https://developer.apple.com/documentation/swiftui/imagerenderer#Rendering-to-a-PDF-context, that API might be very useful here.

In addition to that, I would suggest to investigate

We can then add an additional PDF page with the signature and names using a custom view that we render in a PDF and append it at the end using https://developer.apple.com/documentation/swiftui/imagerenderer#Rendering-to-a-PDF-context.

vishnuravi commented 9 months ago

I deleted the create-pdf branch on the https://github.com/StanfordSpezi/Spezi repository. The approach I was trying there doesn't work. I would start over with this new approach you have suggested @PSchmiedmayer . @philippzagar are you interested in trying this out?

philippzagar commented 9 months ago

Sorry for the late response, I was sick the last couple of days.

Thanks @PSchmiedmayer for adding lots of additional resources that may be useful! 🚀 Also, thanks for clarifying to not use a closure-based approach but to add the exported file to the Standard 👍

@vishnuravi I already built an implementation that enables the export of MD onboarding texts including signature. However, I'm not yet happy with the API design there, will look at that closer this and next week. The goal is to merge a first implementation (probably only for MD at first) next week. Sadly, the SwiftUI ImageRenderer has quite a lot of limitations actually rendering larger/more complex SwiftUI views, therefore a UIKit-based approach (as @PSchmiedmayer outlined) might be the right approach here. Will investigate that.

PSchmiedmayer commented 9 months ago

Sounds great; thank you @philippzagar! 👍