Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: implement pdf note export

thecodrrf4e80d4dcca7
diff --git a/apps/web/package.json b/apps/web/package.json
index e30d17f16..09134f2e8 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -19,7 +19,7 @@
"immer": "^8.0.1",
"localforage": "^1.7.3",
"localforage-getitems": "https://github.com/thecodrr/localForage-getItems.git",
- "notes-core": "npm:@streetwriters/notesnook-core@latest",
+ "print-js": "^1.6.0",
"qclone": "^1.0.4",
"react": "^17.0.1",
"react-app-polyfill": "^2.0.0",
diff --git a/apps/web/src/common/dialogcontroller.js b/apps/web/src/common/dialogcontroller.js
index 57beb436d..d2a641ec3 100644
--- a/apps/web/src/common/dialogcontroller.js
+++ b/apps/web/src/common/dialogcontroller.js
@@ -256,6 +256,15 @@ export function showExportDialog(noteIds) {
icon={Icon.Export}
onClose={() => perform(false)}
exportNote={async (format) => {
+ if (format === "pdf") {
+ if (noteIds.length > 1)
+ showToast("error", "Multiple notes cannot be exported as PDF.");
+ const note = db.notes.note(noteIds[0]);
+ await exportToPDF(await note.export("html"));
+ perform(true);
+ return;
+ }
+
var files = [];
for (var noteId of noteIds) {
const note = db.notes.note(noteId);
@@ -276,6 +285,41 @@ export function showExportDialog(noteIds) {
));
}
+async function exportToPDF(content) {
+ return new Promise((resolve) => {
+ import("print-js").then(async ({ default: printjs }) => {
+ printjs({
+ printable: content,
+ type: "raw-html",
+ onPrintDialogClose: () => {
+ resolve();
+ },
+ });
+
+ // TODO
+ // const doc = new jsPDF("p", "px", "letter");
+ // const div = document.createElement("div");
+ // const { width, height } = doc.internal.pageSize;
+ // div.innerHTML = content;
+ // div.style.width = width - 80 + "px";
+ // div.style.height = height - 80 + "px";
+ // div.style.position = "absolute";
+ // div.style.top = 0;
+ // div.style.left = 0;
+ // div.style.margin = "40px";
+ // div.style.fontSize = "11px";
+ // document.body.appendChild(div);
+
+ // await doc.html(div, {
+ // callback: async (doc) => {
+ // div.remove();
+ // resolve(doc.output());
+ // },
+ // });
+ });
+ });
+}
+
export function showForgotPasswordDialog() {
return showDialog((Dialogs, perform) => (
<Dialogs.ForgotPasswordDialog onClose={() => perform()} />
diff --git a/apps/web/src/components/dialogs/exportdialog.js b/apps/web/src/components/dialogs/exportdialog.js
index 682f3bcf9..55b70e743 100644
--- a/apps/web/src/components/dialogs/exportdialog.js
+++ b/apps/web/src/components/dialogs/exportdialog.js
@@ -4,6 +4,13 @@ import * as Icon from "../icons";
import Dialog from "./dialog";
const formats = [
+ {
+ type: "pdf",
+ title: "PDF",
+ icon: Icon.PDF,
+ subtitle:
+ "Can be opened in any PDF reader like Adobe Acrobat or Foxit Reader.",
+ },
{
type: "md",
title: "Markdown",
@@ -14,7 +21,7 @@ const formats = [
type: "html",
title: "HTML",
icon: Icon.HTML,
- subtitle: "Can be opened in any browser.",
+ subtitle: "Can be opened in any web browser like Google Chrome.",
},
{
type: "txt",
@@ -29,7 +36,7 @@ function ExportDialog(props) {
isOpen={true}
title={props.title}
icon={props.icon}
- description="You can export your note to Markdown, HTML, or Text."
+ description="You can export your note to Markdown, HTML, PDF or Text."
onClose={props.onClose}
scrollable
negativeButton={{
diff --git a/apps/web/src/components/icons/index.js b/apps/web/src/components/icons/index.js
index ee45c0266..55c03dea8 100644
--- a/apps/web/src/components/icons/index.js
+++ b/apps/web/src/components/icons/index.js
@@ -103,6 +103,7 @@ export const Properties = createIcon(Icons.mdiDotsVertical);
// FORMATS
export const Markdown = createIcon(Icons.mdiLanguageMarkdownOutline);
+export const PDF = createIcon(Icons.mdiFilePdfOutline);
export const HTML = createIcon(Icons.mdiLanguageHtml5);
export const Text = createIcon(Icons.mdiFormatTitle);
diff --git a/apps/web/src/utils/download.js b/apps/web/src/utils/download.js
index 0a2b73689..fb7c0da1f 100644
--- a/apps/web/src/utils/download.js
+++ b/apps/web/src/utils/download.js
@@ -4,6 +4,7 @@ const map = {
md: "text/markdown",
html: "text/html",
txt: "text/plain",
+ pdf: "application/pdf",
};
export default function download(filename, text, type) {
diff --git a/apps/web/yarn.lock b/apps/web/yarn.lock
index 2b518aafb..9ca325fe3 100644
--- a/apps/web/yarn.lock
+++ b/apps/web/yarn.lock
@@ -9807,6 +9807,11 @@ pretty-format@^26.0.0, pretty-format@^26.6.0, pretty-format@^26.6.2:
ansi-styles "^4.0.0"
react-is "^17.0.1"
+print-js@^1.6.0:
+ version "1.6.0"
+ resolved "https://registry.yarnpkg.com/print-js/-/print-js-1.6.0.tgz#692b046cf31992b46afa6c6d8a9db1c69d431d1f"
+ integrity sha512-BfnOIzSKbqGRtO4o0rnj/K3681BSd2QUrsIZy/+WdCIugjIswjmx3lDEZpXB2ruGf9d4b3YNINri81+J0FsBWg==
+
process-nextick-args@~2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"