|
32 | 32 | modifier="⌘ + ⇧ + s "
|
33 | 33 | @click="handleSaveAsHTML"
|
34 | 34 | />
|
| 35 | + <MenuItem label="Save File as PDF" @click="handleSaveAsPDF" /> |
| 36 | + <MenuItem label="Save File as Image" @click="handleSaveAsImage" /> |
35 | 37 | </Menu>
|
36 | 38 |
|
37 | 39 | <Button
|
@@ -66,6 +68,7 @@ import { copy } from "../lib/copy";
|
66 | 68 | import { defaultMarkdownText } from "../resources/default-md";
|
67 | 69 | import { reactive, onMounted, ref, onUnmounted } from "vue";
|
68 | 70 | import marked from "../lib/marked";
|
| 71 | +import html2pdf from "html2pdf.js"; |
69 | 72 |
|
70 | 73 | const toastRef = ref(null);
|
71 | 74 |
|
@@ -148,14 +151,78 @@ function handleSaveFile() {
|
148 | 151 | exportFile(fileNameWithExt, file);
|
149 | 152 | }
|
150 | 153 |
|
| 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 | +
|
151 | 212 | function createFile(data) {
|
152 | 213 | return new Blob([data], { type: "text/plain" });
|
153 | 214 | }
|
154 | 215 |
|
155 |
| -function exportFile(filename, file) { |
| 216 | +function exportFile(filename, file, generateDataURI = true) { |
156 | 217 | const a = document.createElement("a");
|
157 | 218 | 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; |
159 | 226 | a.download = filename;
|
160 | 227 | a.click();
|
161 | 228 | document.body.removeChild(a);
|
|
0 commit comments