Skip to content

Commit 5d5a598

Browse files
committed
Prior to recording... may make one more change beforehand still...
1 parent 30f3086 commit 5d5a598

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/lib/FetchAD.svelte

+36
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,30 @@
186186
});
187187
}
188188
189+
async function injectPdfIntoDocRef(url, attachment) {
190+
try {
191+
const response = await fetch(url);
192+
if (response.ok) {
193+
const blob = await response.blob();
194+
const reader = new FileReader();
195+
reader.onloadend = () => {
196+
console.log(reader.result); // Log the full data URL for debugging
197+
attachment.data = reader.result.split(',')[1]; // Base64 encoded data
198+
};
199+
reader.readAsDataURL(blob);
200+
/**
201+
const arrayBuffer = await response.arrayBuffer();
202+
const base64String = btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
203+
attachment.data = base64String;
204+
*/
205+
} else {
206+
console.error(`Failed to fetch PDF from ${url}`);
207+
}
208+
} catch (error) {
209+
console.error(`Error fetching PDF from ${url}:`, error);
210+
}
211+
}
212+
189213
async function prepareIps() {
190214
fetchError = '';
191215
processing = true;
@@ -210,6 +234,18 @@
210234
// Filter out resources that don't have a category
211235
resources = resources.filter(nonSignatureDR);
212236
237+
// if one of the DR's `content` elements has attachment.contentType = 'application/pdf', download if possible, put base64 of pdf in DR.content.attachment.data
238+
const hasPdfContent = dr => dr.content && dr.content.some(content => content.attachment && content.attachment.contentType === 'application/pdf');
239+
240+
resources.forEach(async dr => {
241+
if (hasPdfContent(dr)) {
242+
const pdfContent = dr.content.find(content => content.attachment && content.attachment.contentType === 'application/pdf');
243+
if (pdfContent && pdfContent.attachment && pdfContent.attachment.url) {
244+
await injectPdfIntoDocRef (pdfContent.attachment.url, pdfContent.attachment);
245+
}
246+
}
247+
});
248+
213249
resources.unshift(patient);
214250
215251
result = {

src/lib/resource-templates/AdvanceDirective.svelte

+9-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@ docStatus:
5252
{#if resource.docStatus}
5353
{resource.docStatus}
5454
{/if}
55-
<br />
5655
{#if resource.description && resource.description.text}
56+
<br />
5757
{resource.description.text}
5858
{/if}
59+
<br/>
60+
{#if resource.content}
61+
{#each resource.content as content}
62+
{#if content.attachment && content.attachment.data}
63+
PDF present: <a href={"data:application/pdf;base64," + content.attachment.data} target="_blank" rel="noopener noreferrer">View</a>
64+
{/if}
65+
{/each}
66+
{/if}

0 commit comments

Comments
 (0)