Skip to content

Commit 7504198

Browse files
authored
Merge pull request #83 from the-commons-project/save-as-PDF
Save-as-PDF
2 parents 37ba063 + b655d03 commit 7504198

File tree

4 files changed

+119
-16
lines changed

4 files changed

+119
-16
lines changed

package-lock.json

+92-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"fhirclient": "^2.5.2",
1313
"html2canvas": "^1.4.1",
1414
"jose": "^4.13.1",
15+
"jspdf": "^2.5.1",
1516
"pdfjs-dist": "^3.3.122",
1617
"qr-scanner": "^1.4.2",
1718
"react": "^18.2.0",

src/Data.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Button, TextField, Select, MenuItem } from '@mui/material';
33
import CircularProgress from '@mui/material/CircularProgress';
44
import { useOptionalFhir } from './OptionalFhir';
55
import { verifySHX, SHX_STATUS_NEED_PASSCODE, SHX_STATUS_OK } from './lib/SHX.js';
6-
import { saveDivToFile, saveDivToFHIR, downloadBundleToJSON } from './lib/saveDiv.js';
6+
import { saveDivToPdfFile, saveDivToFHIR, downloadBundleToJSON } from './lib/saveDiv.js';
77
import { getDeferringCodeRenderer } from './lib/codes.js';
88
import * as res from './lib/resources.js';
99
import ValidationInfo from './ValidationInfo.js';
@@ -128,7 +128,7 @@ export default function Data({ shx }) {
128128
{ elt }
129129
</div>
130130
<div>
131-
{ elt && <Button onClick={ () => onSaveClick(true) }>save to file</Button> }
131+
{ elt && <Button onClick={ () => onSaveClick(true) }>save to PDF</Button> }
132132
{ elt && fhir && <Button onClick={ () => onSaveClick(false) }>save to ehr</Button> }
133133
{ elt && <Button onClick={ () => downloadBundleToJSON(bundle.fhir, "fhir-bundle-data") }>Save as FHIR</Button> }
134134
<Button onClick={ () => setShowSource(!showSource) }>source</Button>
@@ -154,7 +154,7 @@ export default function Data({ shx }) {
154154
const div = document.getElementById("bundle-contents");
155155

156156
if (toFile) {
157-
saveDivToFile(div, baseName);
157+
saveDivToPdfFile(div, baseName);
158158
}
159159
else {
160160
saveDivToFHIR(fhir, div, baseName);

src/lib/saveDiv.js

+23-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
1-
21
import html2canvas from 'html2canvas';
2+
import { jsPDF } from 'jspdf';
33

4-
// +---------------+
5-
// | saveDivToFile |
6-
// +---------------+
7-
8-
export async function saveDivToFile(div, baseName) {
4+
// +-------------------+
5+
// | saveDivToPdfFile |
6+
// +-------------------+
97

8+
export async function saveDivToPdfFile(div, baseName) {
9+
// Use the divToImage helper function to convert the div to image data
1010
const imageInfo = await divToImage(div);
11-
12-
const link = document.createElement("a");
13-
link.href = imageInfo.url;
14-
link.download = getFilename(baseName, imageInfo.extension);
1511

16-
link.click();
12+
// Calculate the number of pages using the width and height from imageInfo
13+
const pdfWidth = imageInfo.width * 0.264583; // Width in mm (1px = 0.264583mm based on 96dpi)
14+
const pdfHeight = imageInfo.height * 0.264583; // Height in mm
15+
16+
// Create a new jsPDF instance
17+
const pdf = new jsPDF({
18+
orientation: pdfWidth > pdfHeight ? 'l' : 'p',
19+
unit: 'mm',
20+
format: [pdfWidth, pdfHeight]
21+
});
22+
23+
// Add the image to the PDF using the data URL
24+
pdf.addImage(imageInfo.url, 'PNG', 0, 0, pdfWidth, pdfHeight);
25+
26+
// Save the PDF
27+
pdf.save(`${baseName}.pdf`);
1728
}
1829

30+
1931
// +---------------+
2032
// | saveDivToFHIR |
2133
// +---------------+

0 commit comments

Comments
 (0)