Skip to content

Commit 9aa2bfc

Browse files
Merge pull request #1959 from amin-dev-tech/fix-pdf
fix the corrupted pdf export on Save Text as File project
2 parents 1dbf7bc + 2002f16 commit 9aa2bfc

2 files changed

Lines changed: 39 additions & 12 deletions

File tree

Projects/Save Text as File/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<meta http-equiv="X-UA-Compatible" content="IE=edge">
67
<meta name="viewport" content="width=device-width, initial-scale=1.0">
78
<title>Save Text As File</title>
89
<link rel="stylesheet" href="style.css">
10+
<script src="https://unpkg.com/pdf-lib@1.4.0/dist/pdf-lib.min.js"></script>
911
</head>
12+
1013
<body>
1114
<div class="wrapper">
1215
<textarea required placeholder="Enter your text here"></textarea>
@@ -35,4 +38,5 @@
3538
</div>
3639
<script src="script.js"></script>
3740
</body>
41+
3842
</html>
Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,42 @@
11
const textarea = document.querySelector("textarea"),
2-
fileNameInput = document.querySelector(".file-name input"),
3-
selectMenu = document.querySelector(".save-as select"),
4-
saveBtn = document.querySelector(".save-btn");
2+
fileNameInput = document.querySelector(".file-name input"),
3+
selectMenu = document.querySelector(".save-as select"),
4+
saveBtn = document.querySelector(".save-btn");
55

66
selectMenu.addEventListener("change", () => {
7-
let selectOption = selectMenu.options[selectMenu.selectedIndex].text;
8-
saveBtn.innerText = `Save as ${selectOption.split(" ")[0]} File`;
9-
})
7+
let selectOption = selectMenu.options[selectMenu.selectedIndex].text;
8+
saveBtn.innerText = `Save as ${selectOption.split(" ")[0]} File`;
9+
});
1010

11-
saveBtn.addEventListener("click", () => {
12-
const blob = new Blob([textarea.value], {type: selectMenu.value});
11+
saveBtn.addEventListener("click", async () => {
12+
const content = textarea.value;
13+
const contentType = selectMenu.value;
14+
const fileName = fileNameInput.value;
15+
const link = document.createElement("a");
16+
link.download = fileName;
17+
18+
// if the format is .pdf use the pdf-lib package otherwise use the default code
19+
if (selectMenu.value === "application/pdf") {
20+
const pdfDoc = await PDFLib.PDFDocument.create();
21+
const page = pdfDoc.addPage();
22+
const { width, height } = page.getSize();
23+
24+
page.drawText(content, {
25+
x: width - 540,
26+
y: height - 55,
27+
size: 12,
28+
});
29+
// console.log(width);
30+
// console.log(height);
31+
const pdfBytes = await pdfDoc.save();
32+
const blob = new Blob([pdfBytes], { type: contentType });
33+
const fileUrl = URL.createObjectURL(blob);
34+
link.href = fileUrl;
35+
link.click();
36+
} else {
37+
const blob = new Blob([content], { type: contentType });
1338
const fileUrl = URL.createObjectURL(blob);
14-
const link = document.createElement("a");
15-
link.download = fileNameInput.value;
1639
link.href = fileUrl;
1740
link.click();
18-
// console.log(blob)
19-
})
41+
}
42+
});

0 commit comments

Comments
 (0)