Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: add convert pdf function

Marc Seitzd86e3a571ab6
diff --git a/package-lock.json b/package-lock.json
index 4c2da930..758db45c 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -52,6 +52,7 @@
"js-cookie": "^3.0.5",
"lucide-react": "^0.284.0",
"ms": "^2.1.3",
+ "mupdf": "^0.1.1",
"nanoid": "^5.0.1",
"next": "^13.5.4",
"next-auth": "^4.23.2",
@@ -7748,6 +7749,11 @@
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="
},
+ "node_modules/mupdf": {
+ "version": "0.1.1",
+ "resolved": "https://registry.npmjs.org/mupdf/-/mupdf-0.1.1.tgz",
+ "integrity": "sha512-ue/mFh//qbZHNB/A6gBs1BxlxEFyVhB13lORbtvOY0Rzsj8xbsH5V9FBWLIhp4cDs27KTytPoaw5e6BEfKKncA=="
+ },
"node_modules/mz": {
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/mz/-/mz-2.7.0.tgz",
@@ -16419,6 +16425,11 @@
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="
},
+ "mupdf": {
+ "version": "0.1.1",
+ "resolved": "https://registry.npmjs.org/mupdf/-/mupdf-0.1.1.tgz",
+ "integrity": "sha512-ue/mFh//qbZHNB/A6gBs1BxlxEFyVhB13lORbtvOY0Rzsj8xbsH5V9FBWLIhp4cDs27KTytPoaw5e6BEfKKncA=="
+ },
"mz": {
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/mz/-/mz-2.7.0.tgz",
diff --git a/package.json b/package.json
index 8250a540..18b34a26 100644
--- a/package.json
+++ b/package.json
@@ -58,6 +58,7 @@
"js-cookie": "^3.0.5",
"lucide-react": "^0.284.0",
"ms": "^2.1.3",
+ "mupdf": "^0.1.1",
"nanoid": "^5.0.1",
"next": "^13.5.4",
"next-auth": "^4.23.2",
diff --git a/pages/api/mupdf/convert-page.ts b/pages/api/mupdf/convert-page.ts
new file mode 100644
index 00000000..306939b0
--- /dev/null
+++ b/pages/api/mupdf/convert-page.ts
@@ -0,0 +1,57 @@
+import { NextApiRequest, NextApiResponse } from "next";
+// @ts-ignore
+import mupdf from "mupdf";
+import { put } from "@vercel/blob";
+import prisma from "@/lib/prisma";
+
+export default async (req: NextApiRequest, res: NextApiResponse) => {
+ // check if post method
+ if (req.method !== "POST") {
+ res.status(405).json({ error: "Method Not Allowed" });
+ return;
+ }
+
+ try {
+ const { documentVersionId, pageNumber, url } = req.body as {
+ documentVersionId: string;
+ pageNumber: number;
+ url: string;
+ };
+
+ // Fetch the PDF data
+ const response = await fetch(url);
+ // Convert the response to an ArrayBuffer
+ const pdfData = await response.arrayBuffer();
+ // Create a MuPDF instance
+ var doc = mupdf.Document.openDocument(pdfData, "application/pdf");
+
+ var page = doc.loadPage(pageNumber-1); // 0-based page index
+ var pixmap = page.toPixmap(
+ mupdf.Matrix.identity,
+ mupdf.ColorSpace.DeviceRGB
+ );
+ var pngBuffer = pixmap.asPNG();
+ const blob = await put(`page-${pageNumber}.png`, pngBuffer, {
+ access: "public",
+ });
+
+ const documentPage = await prisma.documentPage.create({
+ data: {
+ versionId: documentVersionId,
+ pageNumber: pageNumber,
+ file: blob.url,
+ },
+ });
+
+ if (!documentPage) {
+ res.status(500).json({ error: "Failed to create document page" });
+ return;
+ }
+
+ // Send the images as a response
+ res.status(200).json({ documentPageId: documentPage.id });
+ } catch (error) {
+ console.error("Error:", error);
+ res.status(500).json({ error: "Internal Server Error" });
+ }
+};
diff --git a/pages/api/mupdf/get-pages.ts b/pages/api/mupdf/get-pages.ts
new file mode 100644
index 00000000..a7a399d8
--- /dev/null
+++ b/pages/api/mupdf/get-pages.ts
@@ -0,0 +1,30 @@
+import { NextApiRequest, NextApiResponse } from "next";
+// @ts-ignore
+import mupdf from "mupdf";
+
+export default async (req: NextApiRequest, res: NextApiResponse) => {
+ // check if post method
+ if (req.method !== "POST") {
+ res.status(405).json({ error: "Method Not Allowed" });
+ return;
+ }
+
+ try {
+
+ const { url } = req.body as { url: string };
+ // Fetch the PDF data
+ const response = await fetch(url);
+ // Convert the response to an ArrayBuffer
+ const pdfData = await response.arrayBuffer();
+ // Create a MuPDF instance
+ var doc = mupdf.Document.openDocument(pdfData, "application/pdf");
+
+ var n = doc.countPages();
+
+ // Send the images as a response
+ res.status(200).json({ numPages: n });
+ } catch (error) {
+ console.error("Error:", error);
+ res.status(500).json({ error: "Internal Server Error" });
+ }
+};