Skip to content

Commit e5994c2

Browse files
committed
add export to PDF,JPEG
#4
1 parent 0782a2a commit e5994c2

File tree

4 files changed

+680
-5
lines changed

4 files changed

+680
-5
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@
1010
"dependencies": {
1111
"codejar": "^3.5.0",
1212
"highlight.js": "^11.2.0",
13+
"html2pdf.js": "0.9.3",
1314
"marked": "^3.0.2",
1415
"vue": "^3.0.5"
1516
},
1617
"devDependencies": {
17-
"tailwindcss": "^2.2.7",
1818
"@vitejs/plugin-vue": "^1.3.0",
1919
"@vue/compiler-sfc": "^3.0.5",
2020
"autoprefixer": "^10.3.2",
2121
"cssnano": "^5.0.8",
2222
"postcss": "^8.3.6",
23+
"tailwindcss": "^2.2.7",
2324
"vite": "^2.4.4"
2425
}
2526
}

src/lib/marked/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import hljs from "highlight.js";
44

55
marked.setOptions({
66
highlight: function (code, lang, callback) {
7-
console.log({ code, lang, callback });
87
const highlightedCode = hljs.highlight(code, {
98
language: lang || "plaintext",
109
}).value;

src/pages/Home.vue

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
modifier="⌘ + ⇧ + s "
3333
@click="handleSaveAsHTML"
3434
/>
35+
<MenuItem label="Save File as PDF" @click="handleSaveAsPDF" />
36+
<MenuItem label="Save File as Image" @click="handleSaveAsImage" />
3537
</Menu>
3638

3739
<Button
@@ -66,6 +68,7 @@ import { copy } from "../lib/copy";
6668
import { defaultMarkdownText } from "../resources/default-md";
6769
import { reactive, onMounted, ref, onUnmounted } from "vue";
6870
import marked from "../lib/marked";
71+
import html2pdf from "html2pdf.js";
6972
7073
const toastRef = ref(null);
7174
@@ -148,14 +151,78 @@ function handleSaveFile() {
148151
exportFile(fileNameWithExt, file);
149152
}
150153
154+
async function handleSaveAsPDF() {
155+
try {
156+
const fileName = prompt("Name your file", "mark.pdf");
157+
if (!fileName) {
158+
return;
159+
}
160+
state.showPreview = true;
161+
const htmlString = marked(state.code);
162+
const options = {
163+
margin: 0.25,
164+
filename: fileName,
165+
jsPDF: { unit: "in", format: "a4", orientation: "portrait" },
166+
};
167+
setTimeout(() => {
168+
html2pdf()
169+
.set(options)
170+
.from(htmlString, "string")
171+
.to("pdf")
172+
.save()
173+
.then(() => {
174+
state.showPreview = false;
175+
});
176+
}, 250);
177+
} catch (err) {
178+
console.error(err);
179+
}
180+
}
181+
async function handleSaveAsImage() {
182+
try {
183+
const fileName = prompt("Name your file", "mark.jpeg");
184+
if (!fileName) {
185+
return;
186+
}
187+
state.showPreview = true;
188+
const htmlString = marked(state.code);
189+
const options = {
190+
margin: 0.25,
191+
filename: fileName,
192+
image: { type: "jpeg", quality: 0.98 },
193+
};
194+
195+
setTimeout(() => {
196+
html2pdf()
197+
.set(options)
198+
.from(htmlString, "string")
199+
.to("container")
200+
.toImg()
201+
.outputImg("datauri")
202+
.then((dataURL) => {
203+
exportFile(fileName,dataURL,false)
204+
state.showPreview = false;
205+
})
206+
}, 250);
207+
} catch (err) {
208+
console.error(err);
209+
}
210+
}
211+
151212
function createFile(data) {
152213
return new Blob([data], { type: "text/plain" });
153214
}
154215
155-
function exportFile(filename, file) {
216+
function exportFile(filename, file, generateDataURI = true) {
156217
const a = document.createElement("a");
157218
document.body.appendChild(a);
158-
a.href = window.URL.createObjectURL(file);
219+
let uri;
220+
if (generateDataURI) {
221+
uri = window.URL.createObjectURL(file);
222+
} else {
223+
uri = file;
224+
}
225+
a.href = uri;
159226
a.download = filename;
160227
a.click();
161228
document.body.removeChild(a);

0 commit comments

Comments
 (0)