Skip to content

Commit

Permalink
Prior to recording... may make one more change beforehand still...
Browse files Browse the repository at this point in the history
  • Loading branch information
mcjustin committed May 19, 2024
1 parent 30f3086 commit 5d5a598
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/lib/FetchAD.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,30 @@
});
}
async function injectPdfIntoDocRef(url, attachment) {
try {
const response = await fetch(url);
if (response.ok) {
const blob = await response.blob();
const reader = new FileReader();
reader.onloadend = () => {
console.log(reader.result); // Log the full data URL for debugging
attachment.data = reader.result.split(',')[1]; // Base64 encoded data
};
reader.readAsDataURL(blob);
/**
const arrayBuffer = await response.arrayBuffer();
const base64String = btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
attachment.data = base64String;
*/
} else {
console.error(`Failed to fetch PDF from ${url}`);
}
} catch (error) {
console.error(`Error fetching PDF from ${url}:`, error);
}
}
async function prepareIps() {
fetchError = '';
processing = true;
Expand All @@ -210,6 +234,18 @@
// Filter out resources that don't have a category
resources = resources.filter(nonSignatureDR);
// 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
const hasPdfContent = dr => dr.content && dr.content.some(content => content.attachment && content.attachment.contentType === 'application/pdf');
resources.forEach(async dr => {
if (hasPdfContent(dr)) {
const pdfContent = dr.content.find(content => content.attachment && content.attachment.contentType === 'application/pdf');
if (pdfContent && pdfContent.attachment && pdfContent.attachment.url) {
await injectPdfIntoDocRef (pdfContent.attachment.url, pdfContent.attachment);
}
}
});
resources.unshift(patient);
result = {
Expand Down
10 changes: 9 additions & 1 deletion src/lib/resource-templates/AdvanceDirective.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ docStatus:
{#if resource.docStatus}
{resource.docStatus}
{/if}
<br />
{#if resource.description && resource.description.text}
<br />
{resource.description.text}
{/if}
<br/>
{#if resource.content}
{#each resource.content as content}
{#if content.attachment && content.attachment.data}
PDF present: <a href={"data:application/pdf;base64," + content.attachment.data} target="_blank" rel="noopener noreferrer">View</a>
{/if}
{/each}
{/if}

0 comments on commit 5d5a598

Please sign in to comment.