Skip to content

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

fix: download watermarked document of pptx/docx

Marc Seitz72e49b49a412
diff --git a/components/documents/document-header.tsx b/components/documents/document-header.tsx
index 5ab415fa..e6da0d65 100644
--- a/components/documents/document-header.tsx
+++ b/components/documents/document-header.tsx
@@ -326,17 +326,21 @@ export default function DocumentHeader({
// Create and download the CSV file
const blob = new Blob([data.visits], { type: "text/csv;charset=utf-8;" });
- const url = URL.createObjectURL(blob);
+ const url = window.URL.createObjectURL(blob);
const link = window.document.createElement("a");
link.href = url;
link.setAttribute(
"download",
`${data.documentName}_visits_${formattedTime}.csv`,
);
+ link.rel = "noopener noreferrer";
window.document.body.appendChild(link);
link.click();
- window.document.body.removeChild(link);
- URL.revokeObjectURL(url);
+
+ setTimeout(() => {
+ window.URL.revokeObjectURL(url);
+ window.document.body.removeChild(link);
+ }, 100);
toast.success("CSV file downloaded successfully");
} catch (error) {
diff --git a/components/view/dataroom/document-card.tsx b/components/view/dataroom/document-card.tsx
index 7d4af120..7bcd7619 100644
--- a/components/view/dataroom/document-card.tsx
+++ b/components/view/dataroom/document-card.tsx
@@ -6,6 +6,8 @@ import { Download, MoreVerticalIcon } from "lucide-react";
import { useTheme } from "next-themes";
import { toast } from "sonner";
+import { fileIcon } from "@/lib/utils/get-file-icon";
+
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
@@ -15,8 +17,6 @@ import {
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
-import { fileIcon } from "@/lib/utils/get-file-icon";
-
import { DocumentVersion } from "../viewer/dataroom-viewer";
type DRDocument = {
@@ -109,11 +109,14 @@ export default function DocumentCard({
link.download = filenameMatch
? decodeURIComponent(filenameMatch[1])
: document.name;
-
+ link.rel = "noopener noreferrer";
window.document.body.appendChild(link);
link.click();
- window.document.body.removeChild(link);
- window.URL.revokeObjectURL(url);
+
+ setTimeout(() => {
+ window.URL.revokeObjectURL(url);
+ window.document.body.removeChild(link);
+ }, 100);
toast.success("File downloaded successfully");
return;
diff --git a/components/view/nav.tsx b/components/view/nav.tsx
index 4091c4f0..473aa256 100644
--- a/components/view/nav.tsx
+++ b/components/view/nav.tsx
@@ -121,27 +121,38 @@ export default function Nav({
body: JSON.stringify({ linkId, viewId }),
});
+ if (!response.ok) {
+ toast.error("Error downloading file");
+ return;
+ }
+
if (hasWatermark) {
const pdfBlob = await response.blob();
- const blobUrl = URL.createObjectURL(pdfBlob);
+ const blobUrl = window.URL.createObjectURL(pdfBlob);
- const a = document.createElement("a");
- a.href = blobUrl;
- a.download = "watermarked_document.pdf";
- document.body.appendChild(a);
- a.click();
- document.body.removeChild(a);
+ const link = document.createElement("a");
+ link.href = blobUrl;
+ link.rel = "noopener noreferrer";
+ link.download = "watermarked_document.pdf";
+ document.body.appendChild(link);
+ link.click();
- // Clean up the Blob URL
- URL.revokeObjectURL(blobUrl);
+ setTimeout(() => {
+ window.URL.revokeObjectURL(blobUrl);
+ document.body.removeChild(link);
+ }, 100);
} else {
- if (!response.ok) {
- toast.error("Error downloading file");
- return;
- }
const { downloadUrl } = await response.json();
- window.open(downloadUrl, "_blank");
+ const link = document.createElement("a");
+ link.href = downloadUrl;
+ link.rel = "noopener noreferrer";
+ document.body.appendChild(link);
+ link.click();
+
+ setTimeout(() => {
+ document.body.removeChild(link);
+ }, 100);
}
} catch (error) {
console.error("Error downloading file:", error);
diff --git a/pages/api/links/download/dataroom-document.ts b/pages/api/links/download/dataroom-document.ts
index 4cde8259..0b38fa4e 100644
--- a/pages/api/links/download/dataroom-document.ts
+++ b/pages/api/links/download/dataroom-document.ts
@@ -15,7 +15,7 @@ export default async function handle(
res: NextApiResponse,
) {
if (req.method === "POST") {
- // POST /api/links/download/dataroomDocumet
+ // POST /api/links/download/dataroom-document
const { linkId, viewId, documentId } = req.body as {
linkId: string;
viewId: string;
@@ -200,7 +200,7 @@ export default async function handle(
res.setHeader("Content-Type", "application/pdf");
res.setHeader(
"Content-Disposition",
- `attachment; filename="${encodeURIComponent(downloadDocuments[0].document!.name)}"`,
+ 'attachment; filename="watermarked.pdf"',
);
res.setHeader("Content-Length", Buffer.from(pdfBuffer).length);
diff --git a/pages/api/links/download/index.ts b/pages/api/links/download/index.ts
index 18454c52..e901478d 100644
--- a/pages/api/links/download/index.ts
+++ b/pages/api/links/download/index.ts
@@ -1,5 +1,7 @@
import { NextApiRequest, NextApiResponse } from "next";
+import { LinkType } from "@prisma/client";
+
import { getFile } from "@/lib/files/get-file";
import prisma from "@/lib/prisma";
import { getIpAddress } from "@/lib/utils/ip";
@@ -9,7 +11,7 @@ export default async function handle(
res: NextApiResponse,
) {
if (req.method === "POST") {
- // GET /api/links/download
+ // POST /api/links/download
const { linkId, viewId } = req.body as { linkId: string; viewId: string };
try {
@@ -24,6 +26,7 @@ export default async function handle(
viewerEmail: true,
link: {
select: {
+ linkType: true,
allowDownload: true,
expiresAt: true,
isArchived: true,
@@ -78,10 +81,12 @@ export default async function handle(
return res.status(403).json({ error: "Error downloading" });
}
- // if viewedAt is longer than 30 mins ago, we should not allow the download
+ // if viewedAt is longer than 30 mins ago, we should not allow the download for document links and 23 hours ago for dataroom links
if (
- view.viewedAt &&
- view.viewedAt < new Date(Date.now() - 30 * 60 * 1000)
+ (view.link.linkType === LinkType.DOCUMENT_LINK &&
+ view.viewedAt < new Date(Date.now() - 30 * 60 * 1000)) ||
+ (view.link.linkType === LinkType.DATAROOM_LINK &&
+ view.viewedAt < new Date(Date.now() - 23 * 60 * 60 * 1000))
) {
return res.status(403).json({ error: "Error downloading" });
}
@@ -92,11 +97,18 @@ export default async function handle(
data: { downloadedAt: new Date() },
});
+ // get the file to be downloaded, if watermark is enabled and document is not pdf, then get the pdf file, otherwise return the original file
+ // if watermark is enabled and document version is pdf, then get the file
+ // if watermark is not enabled, then get the original file
+ const file =
+ view.link.enableWatermark && view.document!.versions[0].type === "pdf"
+ ? view.document!.versions[0].file
+ : (view.document!.versions[0].originalFile ??
+ view.document!.versions[0].file);
+
const downloadUrl = await getFile({
type: view.document!.versions[0].storageType,
- data:
- view.document!.versions[0].originalFile ??
- view.document!.versions[0].file,
+ data: file,
isDownload: true,
});
@@ -138,8 +150,9 @@ export default async function handle(
"Content-Disposition",
'attachment; filename="watermarked.pdf"',
);
+ res.setHeader("Content-Length", Buffer.from(pdfBuffer).length);
- // Send the buffer directly
+ // Send the watermarked buffer directly
return res.send(Buffer.from(pdfBuffer));
}