Skip to content

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

feat: add watermarking (#576)

Marc Seitz13cd7c555cdd
diff --git a/components/links/link-sheet/index.tsx b/components/links/link-sheet/index.tsx
index d27a9fd6..8ffaa2e1 100644
--- a/components/links/link-sheet/index.tsx
+++ b/components/links/link-sheet/index.tsx
@@ -23,7 +23,7 @@ import {
import { useAnalytics } from "@/lib/analytics";
import { usePlan } from "@/lib/swr/use-billing";
import { useDomains } from "@/lib/swr/use-domains";
-import { LinkWithViews } from "@/lib/types";
+import { LinkWithViews, WatermarkConfig } from "@/lib/types";
import { convertDataUrlToFile, uploadImage } from "@/lib/utils";
import DomainSection from "./domain-section";
@@ -54,6 +54,8 @@ export const DEFAULT_LINK_PROPS = (linkType: LinkType) => ({
enableAgreement: false,
agreementId: null,
showBanner: linkType === LinkType.DOCUMENT_LINK ? true : false,
+ enableWatermark: false,
+ watermarkConfig: null,
});
export type DEFAULT_LINK_TYPE = {
@@ -81,6 +83,8 @@ export type DEFAULT_LINK_TYPE = {
enableAgreement: boolean; // agreement
agreementId: string | null;
showBanner: boolean;
+ enableWatermark: boolean;
+ watermarkConfig: WatermarkConfig | null;
};
export default function LinkSheet({
diff --git a/components/links/link-sheet/link-options.tsx b/components/links/link-sheet/link-options.tsx
index 534846e6..ce7395a9 100644
--- a/components/links/link-sheet/link-options.tsx
+++ b/components/links/link-sheet/link-options.tsx
@@ -22,6 +22,7 @@ import useLimits from "@/lib/swr/use-limits";
import AgreementSection from "./agreement-section";
import QuestionSection from "./question-section";
import ScreenshotProtectionSection from "./screenshot-protection-section";
+import WatermarkSection from "./watermark-section";
export type LinkUpgradeOptions = {
state: boolean;
@@ -131,6 +132,11 @@ export const LinkOptions = ({
}
handleUpgradeStateChange={handleUpgradeStateChange}
/>
+ <WatermarkSection
+ {...{ data, setData }}
+ isAllowed={isTrial || isDatarooms}
+ handleUpgradeStateChange={handleUpgradeStateChange}
+ />
<AgreementSection
{...{ data, setData }}
isAllowed={isTrial || isDatarooms}
diff --git a/components/links/link-sheet/watermark-panel/index.tsx b/components/links/link-sheet/watermark-panel/index.tsx
new file mode 100644
index 00000000..1e9d1f73
--- /dev/null
+++ b/components/links/link-sheet/watermark-panel/index.tsx
@@ -0,0 +1,309 @@
+import React, { useEffect, useState } from "react";
+
+import { motion } from "framer-motion";
+import { HexColorInput, HexColorPicker } from "react-colorful";
+import { z } from "zod";
+
+import { Button } from "@/components/ui/button";
+import { Checkbox } from "@/components/ui/checkbox";
+import { Input } from "@/components/ui/input";
+import { Label } from "@/components/ui/label";
+import {
+ Popover,
+ PopoverContent,
+ PopoverTrigger,
+} from "@/components/ui/popover";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select";
+import {
+ Sheet,
+ SheetContent,
+ SheetDescription,
+ SheetFooter,
+ SheetHeader,
+ SheetTitle,
+} from "@/components/ui/sheet";
+
+import { FADE_IN_ANIMATION_SETTINGS } from "@/lib/constants";
+import { WatermarkConfig, WatermarkConfigSchema } from "@/lib/types";
+
+interface WatermarkConfigSheetProps {
+ isOpen: boolean;
+ onOpenChange: (open: boolean) => void;
+ initialConfig: Partial<WatermarkConfig>;
+ onSave: (config: WatermarkConfig) => void;
+}
+
+export default function WatermarkConfigSheet({
+ isOpen,
+ onOpenChange,
+ initialConfig,
+ onSave,
+}: WatermarkConfigSheetProps) {
+ const [formValues, setFormValues] =
+ useState<Partial<WatermarkConfig>>(initialConfig);
+ const [errors, setErrors] = useState<Record<string, string>>({});
+
+ useEffect(() => {
+ setFormValues(initialConfig);
+ }, [initialConfig]);
+
+ const handleInputChange = (
+ e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>,
+ ) => {
+ const { name, value } = e.target;
+ setFormValues((prevValues) => ({
+ ...prevValues,
+ [name]: value,
+ }));
+ };
+
+ const validateAndSave = () => {
+ try {
+ const validatedData = WatermarkConfigSchema.parse(formValues);
+ onSave(validatedData);
+ setErrors({});
+ onOpenChange(false);
+ } catch (error) {
+ if (error instanceof z.ZodError) {
+ const fieldErrors: Record<string, string> = {};
+ error.errors.forEach((err) => {
+ if (err.path[0]) {
+ fieldErrors[err.path[0] as string] = err.message;
+ }
+ });
+ setErrors(fieldErrors);
+ }
+ }
+ };
+
+ return (
+ <Sheet open={isOpen} onOpenChange={onOpenChange}>
+ <SheetContent>
+ <SheetHeader>
+ <SheetTitle>Watermark Configuration</SheetTitle>
+ <SheetDescription>
+ Configure the watermark settings for your document.
+ </SheetDescription>
+ </SheetHeader>
+
+ <motion.div
+ className="relative mt-4 space-y-3"
+ {...FADE_IN_ANIMATION_SETTINGS}
+ >
+ <div className="flex w-full flex-col items-start gap-6 overflow-x-visible pb-4 pt-0">
+ <div className="w-full space-y-2">
+ <Label htmlFor="watermark-text">Watermark Text</Label>
+ <Input
+ id="watermark-text"
+ type="text"
+ name="text"
+ placeholder="e.g. Confidential {{email}} {{date}}"
+ value={formValues.text || ""}
+ onChange={handleInputChange}
+ className="focus:ring-inset"
+ />
+ <div className="space-x-1">
+ {["email", "date", "time", "link", "ipAddress"].map((item) => (
+ <Button
+ key={item}
+ size="sm"
+ variant="outline"
+ className="h-7 rounded-3xl bg-muted text-sm font-normal text-foreground/80 hover:bg-muted/70"
+ onClick={(e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ setFormValues((prevValues) => ({
+ ...prevValues,
+ text: `${prevValues.text || ""}{{${item}}}`,
+ }));
+ }}
+ >{`{{${item}}}`}</Button>
+ ))}
+ </div>
+ {errors.text && <p className="text-red-500">{errors.text}</p>}
+ </div>
+
+ <div className="w-full space-y-2">
+ <div className="relative flex items-center space-x-2">
+ <Checkbox
+ id="watermark-tiled"
+ checked={formValues.isTiled}
+ onCheckedChange={(checked) => {
+ setFormValues((prevValues) => ({
+ ...prevValues,
+ isTiled: checked === true,
+ }));
+ }}
+ className="mt-0.5 border border-gray-400 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-gray-300 focus-visible:ring-offset-1 disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:border-white data-[state=checked]:bg-black data-[state=checked]:text-white"
+ />
+ <Label htmlFor="watermark-tiled">Tiled</Label>
+ </div>
+ {errors.isTiled && (
+ <p className="text-red-500">{errors.isTiled}</p>
+ )}
+ </div>
+
+ <div className="w-full space-y-2">
+ <Label htmlFor="watermark-position">Position</Label>
+ <Select
+ name="position"
+ value={formValues.position}
+ disabled={formValues.isTiled}
+ onValueChange={(value) => {
+ setFormValues({
+ ...formValues,
+ position: value as WatermarkConfig["position"],
+ });
+ }}
+ >
+ <SelectTrigger>
+ <SelectValue placeholder="Select position" />
+ </SelectTrigger>
+ <SelectContent>
+ <SelectItem value="top-left">Top Left</SelectItem>
+ <SelectItem value="top-center">Top Center</SelectItem>
+ <SelectItem value="top-right">Top Right</SelectItem>
+ <SelectItem value="middle-left">Middle Left</SelectItem>
+ <SelectItem value="middle-center">Middle Center</SelectItem>
+ <SelectItem value="middle-right">Middle Right</SelectItem>
+ <SelectItem value="bottom-left">Bottom Left</SelectItem>
+ <SelectItem value="bottom-center">Bottom Center</SelectItem>
+ <SelectItem value="bottom-right">Bottom Right</SelectItem>
+ </SelectContent>
+ </Select>
+ {errors.position && (
+ <p className="text-red-500">{errors.position}</p>
+ )}
+ </div>
+
+ <div className="w-full space-y-2">
+ <Label htmlFor="watermark-rotation">Rotation</Label>
+ <Select
+ name="rotation"
+ value={formValues.rotation?.toString()}
+ onValueChange={(value) => {
+ setFormValues({
+ ...formValues,
+ rotation: parseInt(value) as WatermarkConfig["rotation"],
+ });
+ }}
+ >
+ <SelectTrigger>
+ <SelectValue placeholder="Select rotation" />
+ </SelectTrigger>
+ <SelectContent>
+ <SelectItem value="0">0°</SelectItem>
+ <SelectItem value="30">30°</SelectItem>
+ <SelectItem value="45">45°</SelectItem>
+ <SelectItem value="90">90°</SelectItem>
+ <SelectItem value="180">180°</SelectItem>
+ </SelectContent>
+ </Select>
+ {errors.rotation && (
+ <p className="text-red-500">{errors.rotation}</p>
+ )}
+ </div>
+
+ <div className="w-full space-y-2">
+ <Label htmlFor="watermark-color">Text Color</Label>
+ <div className="ml-0.5 mr-0.5 flex space-x-1">
+ <Popover>
+ <PopoverTrigger>
+ <div
+ className="h-9 w-9 cursor-pointer rounded-md shadow-sm ring-1 ring-muted-foreground hover:ring-1 hover:ring-gray-300 focus:ring-inset"
+ style={{ backgroundColor: formValues.color }}
+ />
+ </PopoverTrigger>
+ <PopoverContent>
+ <HexColorPicker
+ color={formValues.color || ""}
+ onChange={(value) => {
+ setFormValues({
+ ...formValues,
+ color: value as WatermarkConfig["color"],
+ });
+ }}
+ />
+ </PopoverContent>
+ </Popover>
+ <HexColorInput
+ className="flex w-full rounded-md border border-input bg-white text-foreground placeholder-muted-foreground focus:border-muted-foreground focus:outline-none focus:ring-inset focus:ring-muted-foreground dark:border-gray-500 dark:bg-gray-800 focus:dark:bg-transparent sm:text-sm"
+ color={formValues.color || ""}
+ onChange={(value) => {
+ setFormValues({
+ ...formValues,
+ color: value as WatermarkConfig["color"],
+ });
+ }}
+ prefixed
+ />
+ </div>
+ {errors.color && <p className="text-red-500">{errors.color}</p>}
+ </div>
+
+ <div className="flex w-full space-x-4">
+ <div className="w-full space-y-2">
+ <Label htmlFor="watermark-fontSize">Font Size</Label>
+ <Input
+ id="watermark-fontSize"
+ type="number"
+ name="fontSize"
+ step="4"
+ value={formValues.fontSize}
+ onChange={(e) => {
+ setFormValues({
+ ...formValues,
+ fontSize: parseInt(
+ e.target.value,
+ ) as WatermarkConfig["fontSize"],
+ });
+ }}
+ className="focus:ring-inset"
+ />
+ {errors.fontSize && (
+ <p className="text-red-500">{errors.fontSize}</p>
+ )}
+ </div>
+ <div className="w-full space-y-2">
+ <Label htmlFor="watermark-opacity">Transparency</Label>
+ <Select
+ name="opacity"
+ value={formValues.opacity?.toString()}
+ onValueChange={(value) => {
+ setFormValues({
+ ...formValues,
+ opacity: parseFloat(value) as WatermarkConfig["opacity"],
+ });
+ }}
+ >
+ <SelectTrigger>
+ <SelectValue placeholder="Select transparency" />
+ </SelectTrigger>
+ <SelectContent>
+ <SelectItem value="1">No transparency</SelectItem>
+ <SelectItem value="0.25">75%</SelectItem>
+ <SelectItem value="0.5">50%</SelectItem>
+ <SelectItem value="0.75">25%</SelectItem>
+ </SelectContent>
+ </Select>
+ {errors.opacity && (
+ <p className="text-red-500">{errors.opacity}</p>
+ )}
+ </div>
+ </div>
+ </div>
+ </motion.div>
+
+ <SheetFooter>
+ <Button onClick={validateAndSave}>Save Watermark</Button>
+ </SheetFooter>
+ </SheetContent>
+ </Sheet>
+ );
+}
diff --git a/components/links/link-sheet/watermark-section.tsx b/components/links/link-sheet/watermark-section.tsx
new file mode 100644
index 00000000..3305efea
--- /dev/null
+++ b/components/links/link-sheet/watermark-section.tsx
@@ -0,0 +1,168 @@
+import { useEffect, useState } from "react";
+
+import { motion } from "framer-motion";
+import { SettingsIcon, StampIcon } from "lucide-react";
+
+import { Button } from "@/components/ui/button";
+import { Input } from "@/components/ui/input";
+import { Label } from "@/components/ui/label";
+
+import { FADE_IN_ANIMATION_SETTINGS } from "@/lib/constants";
+import { WatermarkConfig } from "@/lib/types";
+
+import { DEFAULT_LINK_TYPE } from ".";
+import LinkItem from "./link-item";
+import { LinkUpgradeOptions } from "./link-options";
+import WatermarkConfigSheet from "./watermark-panel";
+
+export default function WatermarkSection({
+ data,
+ setData,
+ isAllowed,
+ handleUpgradeStateChange,
+}: {
+ data: DEFAULT_LINK_TYPE;
+ setData: React.Dispatch<React.SetStateAction<DEFAULT_LINK_TYPE>>;
+ isAllowed: boolean;
+ handleUpgradeStateChange: ({
+ state,
+ trigger,
+ plan,
+ }: LinkUpgradeOptions) => void;
+}) {
+ const { enableWatermark, watermarkConfig } = data;
+ const [enabled, setEnabled] = useState<boolean>(false);
+ const [isConfigOpen, setIsConfigOpen] = useState<boolean>(false);
+
+ useEffect(() => {
+ setEnabled(enableWatermark);
+ }, [enableWatermark]);
+
+ const handleWatermarkToggle = () => {
+ const updatedWatermark = !enabled;
+
+ setData({
+ ...data,
+ enableWatermark: updatedWatermark,
+ watermarkConfig: watermarkConfig || null,
+ });
+ setEnabled(updatedWatermark);
+ };
+
+ const initialconfig: WatermarkConfig = {
+ text: watermarkConfig?.text ?? "",
+ isTiled: watermarkConfig?.isTiled ?? false,
+ opacity: watermarkConfig?.opacity ?? 0.5,
+ color: watermarkConfig?.color ?? "#000000",
+ fontSize: watermarkConfig?.fontSize ?? 24,
+ rotation: watermarkConfig?.rotation ?? 45,
+ position: watermarkConfig?.position ?? "middle-center",
+ };
+
+ const handleConfigSave = (config: WatermarkConfig) => {
+ setData({
+ ...data,
+ watermarkConfig: config,
+ });
+ };
+
+ return (
+ <div className="pb-5">
+ <LinkItem
+ title="Apply Watermark"
+ enabled={enabled}
+ action={handleWatermarkToggle}
+ isAllowed={isAllowed}
+ requiredPlan="datarooms"
+ upgradeAction={() =>
+ handleUpgradeStateChange({
+ state: true,
+ trigger: "link_sheet_watermark_section",
+ plan: "Data Rooms",
+ })
+ }
+ />
+
+ {enabled && (
+ <motion.div
+ className="relative mt-4 space-y-3"
+ {...FADE_IN_ANIMATION_SETTINGS}
+ >
+ <div className="flex w-full flex-col items-start gap-6 overflow-x-visible pt-0">
+ <div className="w-full space-y-2">
+ <Label htmlFor="watermark-text">Watermark Text</Label>
+ <Input
+ id="watermark-text"
+ type="text"
+ name="text"
+ placeholder="e.g. Confidential {{email}} {{date}}"
+ value={watermarkConfig?.text ?? ""}
+ required={enabled}
+ onChange={(e) => {
+ setData((prevData) => ({
+ ...prevData,
+ watermarkConfig: {
+ ...(prevData.watermarkConfig || initialconfig),
+ text: e.target.value,
+ },
+ }));
+ }}
+ className="focus:ring-inset"
+ />
+ <div className="space-x-1">
+ {["email", "date", "time", "link", "ipAddress"].map((item) => (
+ <Button
+ key={item}
+ size="sm"
+ variant="outline"
+ className="h-7 rounded-3xl bg-muted text-sm font-normal text-foreground/80 hover:bg-muted/70"
+ onClick={(e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ setData((prevData) => ({
+ ...prevData,
+ watermarkConfig: {
+ ...(prevData.watermarkConfig || initialconfig),
+ text: `${prevData.watermarkConfig?.text || ""} {{${item}}}`,
+ },
+ }));
+ }}
+ >{`{{${item}}}`}</Button>
+ ))}
+ </div>
+ </div>
+ </div>
+
+ <div className="mt-2 flex w-full items-center justify-between">
+ <p className="text-sm text-muted-foreground">
+ {initialconfig.isTiled ? `tiled` : initialconfig.position},{" "}
+ {initialconfig.rotation}º, {initialconfig.fontSize}px,{" "}
+ {initialconfig.color.toUpperCase()},{" "}
+ {(1 - initialconfig.opacity) * 100}% transparent
+ </p>
+ <Button
+ onClick={(e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ setIsConfigOpen(true);
+ }}
+ variant="outline"
+ className="h-8"
+ size="sm"
+ >
+ <SettingsIcon className="mr-2 h-4 w-4" />
+ Configure
+ </Button>
+ </div>
+ </motion.div>
+ )}
+
+ <WatermarkConfigSheet
+ isOpen={isConfigOpen}
+ onOpenChange={setIsConfigOpen}
+ initialConfig={initialconfig}
+ onSave={handleConfigSave}
+ />
+ </div>
+ );
+}
diff --git a/components/links/links-table.tsx b/components/links/links-table.tsx
index 6d754599..4bd2258d 100644
--- a/components/links/links-table.tsx
+++ b/components/links/links-table.tsx
@@ -33,7 +33,7 @@ import {
} from "@/components/ui/table";
import { usePlan } from "@/lib/swr/use-billing";
-import { LinkWithViews } from "@/lib/types";
+import { LinkWithViews, WatermarkConfig } from "@/lib/types";
import { cn, copyToClipboard, nFormatter, timeAgo } from "@/lib/utils";
import ProcessStatusBar from "../documents/process-status-bar";
@@ -102,6 +102,8 @@ export default function LinksTable({
enableAgreement: link.enableAgreement ? link.enableAgreement : false,
agreementId: link.agreementId,
showBanner: link.showBanner ?? false,
+ enableWatermark: link.enableWatermark ?? false,
+ watermarkConfig: link.watermarkConfig as WatermarkConfig | null,
});
//wait for dropdown to close before opening the link sheet
setTimeout(() => {
diff --git a/components/ui/select.tsx b/components/ui/select.tsx
index d34ce855..dbb9b87f 100644
--- a/components/ui/select.tsx
+++ b/components/ui/select.tsx
@@ -20,7 +20,7 @@ const SelectTrigger = React.forwardRef<
<SelectPrimitive.Trigger
ref={ref}
className={cn(
- "flex w-full items-center justify-between rounded-md border border-input bg-background px-3 py-2 text-sm placeholder:text-muted-foreground focus:outline-none focus:ring-1 focus:ring-ring disabled:cursor-not-allowed disabled:opacity-50",
+ "flex w-full items-center justify-between rounded-md border border-input bg-background px-3 py-2 text-sm placeholder:text-muted-foreground focus:border-muted-foreground focus:outline-none focus:ring-1 focus:ring-inset focus:ring-muted-foreground disabled:cursor-not-allowed disabled:opacity-50 dark:border-gray-500 dark:bg-gray-800 focus:dark:bg-transparent",
className,
)}
{...props}
@@ -41,7 +41,7 @@ const SelectContent = React.forwardRef<
<SelectPrimitive.Content
ref={ref}
className={cn(
- "relative z-50 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
+ "relative z-50 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md focus:ring-inset data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
position === "popper" &&
"data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
className,
diff --git a/components/view/PagesViewerNew.tsx b/components/view/PagesViewerNew.tsx
index 1b1d65cc..eca923dc 100644
--- a/components/view/PagesViewerNew.tsx
+++ b/components/view/PagesViewerNew.tsx
@@ -18,6 +18,7 @@ import {
} from "react-zoom-pan-pinch";
import { toast } from "sonner";
+import { WatermarkConfig } from "@/lib/types";
import { cn } from "@/lib/utils";
import { useMediaQuery } from "@/lib/utils/use-media-query";
@@ -28,6 +29,7 @@ import { PoweredBy } from "./powered-by";
import Question from "./question";
import Toolbar from "./toolbar";
import ViewDurationSummary from "./visitor-graph";
+import { SVGWatermark } from "./watermark-svg";
const DEFAULT_PRELOADED_IMAGES_NUM = 5;
@@ -86,6 +88,9 @@ export default function PagesViewer({
isVertical = false,
viewerEmail,
isPreview,
+ watermarkConfig,
+ ipAddress,
+ linkName,
}: {
pages: {
file: string;
@@ -116,6 +121,9 @@ export default function PagesViewer({
isVertical?: boolean;
viewerEmail?: string;
isPreview?: boolean;
+ watermarkConfig?: WatermarkConfig | null;
+ ipAddress?: string;
+ linkName?: string;
}) {
const router = useRouter();
const { status: sessionStatus } = useSession();
@@ -673,6 +681,7 @@ export default function PagesViewer({
isVertical={isVertical}
isMobile={isMobile}
isPreview={isPreview}
+ hasWatermark={watermarkConfig ? true : false}
/>
<div
style={{ height: "calc(100dvh - 64px)" }}
@@ -778,6 +787,25 @@ export default function PagesViewer({
}
alt={`Page ${index + 1}`}
/>
+
+ {/* Add Watermark Component */}
+ {watermarkConfig ? (
+ <SVGWatermark
+ config={watermarkConfig}
+ viewerData={{
+ email: viewerEmail,
+ date: new Date().toLocaleDateString(),
+ time: new Date().toLocaleTimeString(),
+ link: linkName,
+ ipAddress: ipAddress,
+ }}
+ documentDimensions={
+ imageDimensions[index] || { width: 0, height: 0 }
+ }
+ pageIndex={index}
+ />
+ ) : null}
+
{page.pageLinks ? (
<map name={`page-map-${index + 1}`}>
{page.pageLinks
diff --git a/components/view/dataroom/dataroom-view.tsx b/components/view/dataroom/dataroom-view.tsx
index 4e516def..85b7dcd8 100644
--- a/components/view/dataroom/dataroom-view.tsx
+++ b/components/view/dataroom/dataroom-view.tsx
@@ -16,7 +16,7 @@ import AccessForm, {
} from "@/components/view/access-form";
import { useAnalytics } from "@/lib/analytics";
-import { LinkWithDataroom } from "@/lib/types";
+import { LinkWithDataroom, WatermarkConfig } from "@/lib/types";
import DataroomViewer from "../DataroomViewer";
import PagesViewerNew from "../PagesViewerNew";
@@ -62,6 +62,7 @@ export type DEFAULT_DOCUMENT_VIEW_TYPE = {
sheetData?: SheetData[] | null;
notionData?: { recordMap: ExtendedRecordMap | null };
fileType?: string;
+ ipAddress?: string;
};
export default function DataroomView({
@@ -160,6 +161,7 @@ export default function DataroomView({
sheetData,
fileType,
isPreview,
+ ipAddress,
} = fetchData as DEFAULT_DOCUMENT_VIEW_TYPE;
plausible("dataroomViewed"); // track the event
analytics.identify(
@@ -183,6 +185,7 @@ export default function DataroomView({
sheetData,
fileType,
isPreview,
+ ipAddress,
}));
setSubmitted(true);
setVerificationRequested(false);
@@ -243,6 +246,7 @@ export default function DataroomView({
file: undefined,
viewId: "",
notionData: undefined,
+ ipAddress: undefined,
}));
// This effect is specifically for handling changes to `documentData` post-mount
}, [documentData]);
@@ -350,6 +354,13 @@ export default function DataroomView({
dataroomId={dataroom.id}
setDocumentData={setDocumentData}
isVertical={documentData.isVertical}
+ watermarkConfig={
+ link.enableWatermark
+ ? (link.watermarkConfig as WatermarkConfig)
+ : null
+ }
+ ipAddress={viewData.ipAddress}
+ linkName={link.name ?? `Link #${link.id.slice(-5)}`}
/>
</div>
) : null;
diff --git a/components/view/document-view.tsx b/components/view/document-view.tsx
index 059bcb76..c2462ed8 100644
--- a/components/view/document-view.tsx
+++ b/components/view/document-view.tsx
@@ -14,7 +14,7 @@ import AccessForm, {
} from "@/components/view/access-form";
import { useAnalytics } from "@/lib/analytics";
-import { LinkWithDocument } from "@/lib/types";
+import { LinkWithDocument, WatermarkConfig } from "@/lib/types";
import EmailVerificationMessage from "./email-verification-form";
import ViewData from "./view-data";
@@ -41,6 +41,7 @@ export type DEFAULT_DOCUMENT_VIEW_TYPE = {
sheetData?: SheetData[] | null;
fileType?: string;
isPreview?: boolean;
+ ipAddress?: string;
};
export default function DocumentView({
@@ -129,8 +130,15 @@ export default function DocumentView({
setVerificationRequested(true);
setIsLoading(false);
} else {
- const { viewId, file, pages, sheetData, fileType, isPreview } =
- fetchData as DEFAULT_DOCUMENT_VIEW_TYPE;
+ const {
+ viewId,
+ file,
+ pages,
+ sheetData,
+ fileType,
+ isPreview,
+ ipAddress,
+ } = fetchData as DEFAULT_DOCUMENT_VIEW_TYPE;
plausible("documentViewed"); // track the event
analytics.identify(
userEmail ?? verifiedEmail ?? data.email ?? undefined,
@@ -142,7 +150,15 @@ export default function DocumentView({
viewerId: viewId,
viewerEmail: data.email ?? verifiedEmail ?? userEmail,
});
- setViewData({ viewId, file, pages, sheetData, fileType, isPreview });
+ setViewData({
+ viewId,
+ file,
+ pages,
+ sheetData,
+ fileType,
+ isPreview,
+ ipAddress,
+ });
setSubmitted(true);
setVerificationRequested(false);
setIsLoading(false);
diff --git a/components/view/nav.tsx b/components/view/nav.tsx
index 679d2fc2..aca03bdf 100644
--- a/components/view/nav.tsx
+++ b/components/view/nav.tsx
@@ -57,6 +57,7 @@ export default function Nav({
isVertical,
isMobile,
isPreview,
+ hasWatermark,
}: {
pageNumber?: number;
numPages?: number;
@@ -74,6 +75,7 @@ export default function Nav({
isVertical?: boolean;
isMobile?: boolean;
isPreview?: boolean;
+ hasWatermark?: boolean;
}) {
const downloadFile = async () => {
if (!allowDownload || type === "notion" || isPreview) return;
@@ -86,13 +88,30 @@ 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);
+
+ console.log("Blob URL:", blobUrl);
+
+ const a = document.createElement("a");
+ a.href = blobUrl;
+ a.download = "watermarked_document.pdf";
+ document.body.appendChild(a);
+ a.click();
+ document.body.removeChild(a);
- const { downloadUrl } = await response.json();
- window.open(downloadUrl, "_blank");
+ // Clean up the Blob URL
+ URL.revokeObjectURL(blobUrl);
+ } else {
+ if (!response.ok) {
+ toast.error("Error downloading file");
+ return;
+ }
+ const { downloadUrl } = await response.json();
+
+ window.open(downloadUrl, "_blank");
+ }
} catch (error) {
console.error("Error downloading file:", error);
}
diff --git a/components/view/view-data.tsx b/components/view/view-data.tsx
index 09764849..26a52bdb 100644
--- a/components/view/view-data.tsx
+++ b/components/view/view-data.tsx
@@ -8,7 +8,7 @@ import PDFViewer from "@/components/view/PDFViewer";
import PagesViewerNew from "@/components/view/PagesViewerNew";
import { DEFAULT_DOCUMENT_VIEW_TYPE } from "@/components/view/document-view";
-import { LinkWithDocument } from "@/lib/types";
+import { LinkWithDocument, WatermarkConfig } from "@/lib/types";
import AdvancedExcelViewer from "./viewer/advanced-excel-viewer";
@@ -95,6 +95,11 @@ export default function ViewData({
feedback={link.feedback}
isVertical={document.versions[0].isVertical}
viewerEmail={viewerEmail}
+ watermarkConfig={
+ link.enableWatermark ? (link.watermarkConfig as WatermarkConfig) : null
+ }
+ ipAddress={viewData.ipAddress}
+ linkName={link.name ?? `Link #${link.id.slice(-5)}`}
/>
) : (
<PDFViewer
diff --git a/components/view/watermark-svg.tsx b/components/view/watermark-svg.tsx
new file mode 100644
index 00000000..a0e13736
--- /dev/null
+++ b/components/view/watermark-svg.tsx
@@ -0,0 +1,161 @@
+import React, { useMemo } from "react";
+
+import Handlebars from "handlebars";
+
+import { WatermarkConfig } from "@/lib/types";
+
+export const SVGWatermark = ({
+ config,
+ viewerData,
+ documentDimensions,
+ pageIndex,
+}: {
+ config: WatermarkConfig;
+ viewerData: any;
+ documentDimensions: { width: number; height: number };
+ pageIndex: number;
+}) => {
+ const watermarkText = useMemo(() => {
+ const template = Handlebars.compile(config.text);
+ return template(viewerData);
+ }, [config.text, viewerData]);
+
+ const { width, height } = documentDimensions;
+
+ // Calculate a responsive font size
+ const calculateFontSize = () => {
+ const baseFontSize = Math.min(width, height) * (config.fontSize / 1000);
+ return Math.max(8, Math.min(baseFontSize, config.fontSize)); // Clamp between 8px and config.fontSize
+ };
+
+ const fontSize = calculateFontSize();
+
+ const createPattern = () => {
+ // Estimate text width (this is an approximation)
+ const textWidth = watermarkText.length * fontSize * 0.6;
+
+ // Make pattern size larger than text to avoid cut-off
+ const patternWidth = textWidth;
+ const patternHeight = fontSize * 10;
+
+ return (
+ <pattern
+ id={`watermarkPattern-${pageIndex}`}
+ patternUnits="userSpaceOnUse"
+ width={patternWidth}
+ height={patternHeight}
+ patternTransform={`rotate(-${config.rotation})`}
+ >
+ <text
+ x={patternWidth / 2}
+ y={patternHeight / 4}
+ fontSize={`${fontSize}px`}
+ fill={config.color}
+ opacity={config.opacity}
+ textAnchor="middle"
+ dominantBaseline="middle"
+ >
+ {watermarkText}
+ </text>
+ </pattern>
+ );
+ };
+
+ const createSingleWatermark = () => {
+ let x, y;
+ switch (config.position) {
+ case "top-left":
+ x = fontSize / 2;
+ y = fontSize;
+ break;
+ case "top-center":
+ x = width / 2;
+ y = fontSize;
+ break;
+ case "top-right":
+ x = width - fontSize / 2;
+ y = fontSize;
+ break;
+ case "middle-left":
+ x = fontSize / 2;
+ y = height / 2;
+ break;
+ case "middle-center":
+ x = width / 2;
+ y = height / 2;
+ break;
+ case "middle-right":
+ x = width - fontSize / 2;
+ y = height / 2;
+ break;
+ case "bottom-left":
+ x = fontSize / 2;
+ y = height - fontSize;
+ break;
+ case "bottom-center":
+ x = width / 2;
+ y = height - fontSize;
+ break;
+ case "bottom-right":
+ x = width - fontSize / 2;
+ y = height - fontSize;
+ break;
+ default:
+ x = width / 2;
+ y = height / 2;
+ }
+
+ return (
+ <text
+ x={x}
+ y={y}
+ fontSize={`${fontSize}px`}
+ fill={config.color}
+ opacity={config.opacity}
+ textAnchor={
+ config.position.includes("right")
+ ? "end"
+ : config.position.includes("center")
+ ? "middle"
+ : "start"
+ }
+ dominantBaseline={
+ config.position.includes("top")
+ ? "hanging"
+ : config.position.includes("middle")
+ ? "middle"
+ : "auto"
+ }
+ transform={`rotate(${-config.rotation} ${x} ${y})`}
+ >
+ {watermarkText}
+ </text>
+ );
+ };
+
+ return (
+ <svg
+ width={width}
+ height={height}
+ style={{
+ position: "absolute",
+ top: 0,
+ display: "flex",
+ pointerEvents: "none",
+ }}
+ >
+ {config.isTiled ? (
+ <>
+ {createPattern()}
+ <rect
+ width={width}
+ height={height}
+ fill={`url(#watermarkPattern-${pageIndex})`}
+ />
+ </>
+ ) : (
+ createSingleWatermark()
+ )}
+ </svg>
+ );
+};
diff --git a/lib/types.ts b/lib/types.ts
index e6e7970c..c75779c6 100644
--- a/lib/types.ts
+++ b/lib/types.ts
@@ -11,6 +11,7 @@ import {
View,
} from "@prisma/client";
import { User as NextAuthUser } from "next-auth";
+import { z } from "zod";
export type CustomUser = NextAuthUser & PrismaUser;
@@ -241,3 +242,33 @@ export interface TeamDetail {
userId: string;
}[];
}
+
+export const WatermarkConfigSchema = z.object({
+ text: z.string().min(1, "Text is required."),
+ isTiled: z.boolean(),
+ position: z.enum([
+ "top-left",
+ "top-center",
+ "top-right",
+ "middle-left",
+ "middle-center",
+ "middle-right",
+ "bottom-left",
+ "bottom-center",
+ "bottom-right",
+ ]),
+ rotation: z.union([
+ z.literal(0),
+ z.literal(30),
+ z.literal(45),
+ z.literal(90),
+ z.literal(180),
+ ]),
+ color: z.string().refine((val) => /^#([0-9A-F]{3}){1,2}$/i.test(val), {
+ message: "Invalid color format. Use HEX format like #RRGGBB.",
+ }),
+ fontSize: z.number().min(1, "Font size must be greater than 0."),
+ opacity: z.number().min(0).max(1, "Opacity must be between 0 and 1."),
+});
+
+export type WatermarkConfig = z.infer<typeof WatermarkConfigSchema>;
diff --git a/lib/utils.ts b/lib/utils.ts
index 67083126..d799a6af 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -6,6 +6,7 @@ import crypto from "crypto";
import ms from "ms";
import { customAlphabet } from "nanoid";
import { ThreadMessage } from "openai/resources/beta/threads/messages/messages";
+import { rgb } from "pdf-lib";
import { toast } from "sonner";
import { twMerge } from "tailwind-merge";
@@ -491,3 +492,11 @@ export const sanitizeAllowDenyList = (list: string): string[] => {
.filter((item) => item !== "") // Remove empty items
.filter((item) => emailRegex.test(item) || domainRegex.test(item)); // Remove items that don't match email or domain regex
};
+
+export function hexToRgb(hex: string) {
+ let bigint = parseInt(hex.slice(1), 16);
+ let r = ((bigint >> 16) & 255) / 255; // Convert to 0-1 range
+ let g = ((bigint >> 8) & 255) / 255; // Convert to 0-1 range
+ let b = (bigint & 255) / 255; // Convert to 0-1 range
+ return rgb(r, g, g);
+}
diff --git a/lib/utils/ip.ts b/lib/utils/ip.ts
new file mode 100644
index 00000000..cb8ed1e5
--- /dev/null
+++ b/lib/utils/ip.ts
@@ -0,0 +1,8 @@
+export function getIpAddress(headers: {
+ [key: string]: string | string[] | undefined;
+}): string {
+ if (typeof headers["x-forwarded-for"] === "string") {
+ return (headers["x-forwarded-for"] ?? "127.0.0.1").split(",")[0];
+ }
+ return "127.0.0.1";
+}
diff --git a/next.config.mjs b/next.config.mjs
index 5a346970..3db77dca 100644
--- a/next.config.mjs
+++ b/next.config.mjs
@@ -9,7 +9,8 @@ const nextConfig = {
transpilePackages: ["@trigger.dev/react"],
skipTrailingSlashRedirect: true,
assetPrefix:
- process.env.NODE_ENV === "production"
+ process.env.NODE_ENV === "production" &&
+ process.env.VERCEL_ENV === "production"
? process.env.NEXT_PUBLIC_BASE_URL
: undefined,
async redirects() {
diff --git a/package-lock.json b/package-lock.json
index 1d40fb0b..35d87e5f 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -20,6 +20,7 @@
"@github/webauthn-json": "^2.1.1",
"@jitsu/js": "^1.9.8",
"@next-auth/prisma-adapter": "^1.0.7",
+ "@pdf-lib/fontkit": "^1.1.1",
"@prisma/client": "^5.18.0",
"@radix-ui/react-accordion": "^1.2.0",
"@radix-ui/react-avatar": "^1.1.0",
@@ -66,6 +67,7 @@
"eslint": "8.57.0",
"eslint-config-next": "^14.2.5",
"framer-motion": "^11.3.29",
+ "handlebars": "^4.7.8",
"js-cookie": "^3.0.5",
"lucide-react": "^0.429.0",
"mime-types": "^2.1.35",
@@ -80,6 +82,7 @@
"notion-client": "^6.16.0",
"notion-utils": "^6.16.0",
"openai": "4.20.1",
+ "pdf-lib": "^1.17.1",
"postcss": "^8.4.41",
"posthog-js": "^1.157.2",
"react": "^18.3.1",
@@ -3326,6 +3329,30 @@
"node": ">=0.10"
}
},
+ "node_modules/@pdf-lib/fontkit": {
+ "version": "1.1.1",
+ "resolved": "https://registry.npmjs.org/@pdf-lib/fontkit/-/fontkit-1.1.1.tgz",
+ "integrity": "sha512-KjMd7grNapIWS/Dm0gvfHEilSyAmeLvrEGVcqLGi0VYebuqqzTbgF29efCx7tvx+IEbG3zQciRSWl3GkUSvjZg==",
+ "dependencies": {
+ "pako": "^1.0.6"
+ }
+ },
+ "node_modules/@pdf-lib/standard-fonts": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/@pdf-lib/standard-fonts/-/standard-fonts-1.0.0.tgz",
+ "integrity": "sha512-hU30BK9IUN/su0Mn9VdlVKsWBS6GyhVfqjwl1FjZN4TxP6cCw0jP2w7V3Hf5uX7M0AZJ16vey9yE0ny7Sa59ZA==",
+ "dependencies": {
+ "pako": "^1.0.6"
+ }
+ },
+ "node_modules/@pdf-lib/upng": {
+ "version": "1.0.1",
+ "resolved": "https://registry.npmjs.org/@pdf-lib/upng/-/upng-1.0.1.tgz",
+ "integrity": "sha512-dQK2FUMQtowVP00mtIksrlZhdFXQZPC+taih1q4CvPZ5vqdxR/LKBaFg0oAfzd1GlHZXXSPdQfzQnt+ViGvEIQ==",
+ "dependencies": {
+ "pako": "^1.0.10"
+ }
+ },
"node_modules/@pkgjs/parseargs": {
"version": "0.11.0",
"resolved": "https://registry.npmjs.org/@pkgjs/parseargs/-/parseargs-0.11.0.tgz",
@@ -12054,6 +12081,26 @@
"unenv": "^1.9.0"
}
},
+ "node_modules/handlebars": {
+ "version": "4.7.8",
+ "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.7.8.tgz",
+ "integrity": "sha512-vafaFqs8MZkRrSX7sFVUdo3ap/eNiLnb4IakshzvP56X5Nr1iGKAIqdX6tMlm6HcNRIkr6AxO5jFEoJzzpT8aQ==",
+ "dependencies": {
+ "minimist": "^1.2.5",
+ "neo-async": "^2.6.2",
+ "source-map": "^0.6.1",
+ "wordwrap": "^1.0.0"
+ },
+ "bin": {
+ "handlebars": "bin/handlebars"
+ },
+ "engines": {
+ "node": ">=0.4.7"
+ },
+ "optionalDependencies": {
+ "uglify-js": "^3.1.4"
+ }
+ },
"node_modules/has-bigints": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/has-bigints/-/has-bigints-1.0.2.tgz",
@@ -15640,6 +15687,11 @@
"resolved": "https://registry.npmjs.org/package-json-from-dist/-/package-json-from-dist-1.0.0.tgz",
"integrity": "sha512-dATvCeZN/8wQsGywez1mzHtTlP22H8OEfPrVMLNr4/eGa+ijtLn/6M5f0dY8UKNrC2O9UCU6SSoG3qRKnt7STw=="
},
+ "node_modules/pako": {
+ "version": "1.0.11",
+ "resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz",
+ "integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw=="
+ },
"node_modules/parent-module": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz",
@@ -15749,6 +15801,22 @@
"resolved": "https://registry.npmjs.org/pathe/-/pathe-1.1.2.tgz",
"integrity": "sha512-whLdWMYL2TwI08hn8/ZqAbrVemu0LNaNNJZX73O6qaIdCTfXutsLhMkjdENX0qhsQ9uIimo4/aQOmXkoon2nDQ=="
},
+ "node_modules/pdf-lib": {
+ "version": "1.17.1",
+ "resolved": "https://registry.npmjs.org/pdf-lib/-/pdf-lib-1.17.1.tgz",
+ "integrity": "sha512-V/mpyJAoTsN4cnP31vc0wfNA1+p20evqqnap0KLoRUN0Yk/p3wN52DOEsL4oBFcLdb76hlpKPtzJIgo67j/XLw==",
+ "dependencies": {
+ "@pdf-lib/standard-fonts": "^1.0.0",
+ "@pdf-lib/upng": "^1.0.1",
+ "pako": "^1.0.11",
+ "tslib": "^1.11.1"
+ }
+ },
+ "node_modules/pdf-lib/node_modules/tslib": {
+ "version": "1.14.1",
+ "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz",
+ "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg=="
+ },
"node_modules/pdfjs-dist": {
"version": "3.11.174",
"resolved": "https://registry.npmjs.org/pdfjs-dist/-/pdfjs-dist-3.11.174.tgz",
@@ -19701,6 +19769,18 @@
"resolved": "https://registry.npmjs.org/ufo/-/ufo-1.5.4.tgz",
"integrity": "sha512-UsUk3byDzKd04EyoZ7U4DOlxQaD14JUKQl6/P7wiX4FNvUfm3XL246n9W5AmqwW5RSFJ27NAuM0iLscAOYUiGQ=="
},
+ "node_modules/uglify-js": {
+ "version": "3.19.3",
+ "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.19.3.tgz",
+ "integrity": "sha512-v3Xu+yuwBXisp6QYTcH4UbH+xYJXqnq2m/LtQVWKWzYc1iehYnLixoQDN9FH6/j9/oybfd6W9Ghwkl8+UMKTKQ==",
+ "optional": true,
+ "bin": {
+ "uglifyjs": "bin/uglifyjs"
+ },
+ "engines": {
+ "node": ">=0.8.0"
+ }
+ },
"node_modules/ulid": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/ulid/-/ulid-2.3.0.tgz",
@@ -21581,6 +21661,11 @@
"node": ">=0.10.0"
}
},
+ "node_modules/wordwrap": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-1.0.0.tgz",
+ "integrity": "sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q=="
+ },
"node_modules/wrap-ansi": {
"version": "8.1.0",
"resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-8.1.0.tgz",
diff --git a/package.json b/package.json
index d81cafdb..967a89bb 100644
--- a/package.json
+++ b/package.json
@@ -29,6 +29,7 @@
"@github/webauthn-json": "^2.1.1",
"@jitsu/js": "^1.9.8",
"@next-auth/prisma-adapter": "^1.0.7",
+ "@pdf-lib/fontkit": "^1.1.1",
"@prisma/client": "^5.18.0",
"@radix-ui/react-accordion": "^1.2.0",
"@radix-ui/react-avatar": "^1.1.0",
@@ -75,6 +76,7 @@
"eslint": "8.57.0",
"eslint-config-next": "^14.2.5",
"framer-motion": "^11.3.29",
+ "handlebars": "^4.7.8",
"js-cookie": "^3.0.5",
"lucide-react": "^0.429.0",
"mime-types": "^2.1.35",
@@ -89,6 +91,7 @@
"notion-client": "^6.16.0",
"notion-utils": "^6.16.0",
"openai": "4.20.1",
+ "pdf-lib": "^1.17.1",
"postcss": "^8.4.41",
"posthog-js": "^1.157.2",
"react": "^18.3.1",
diff --git a/pages/api/links/[id]/duplicate.ts b/pages/api/links/[id]/duplicate.ts
index d6415c2e..f536e9b3 100644
--- a/pages/api/links/[id]/duplicate.ts
+++ b/pages/api/links/[id]/duplicate.ts
@@ -1,5 +1,6 @@
import { NextApiRequest, NextApiResponse } from "next";
+import { Prisma } from "@prisma/client";
import { getServerSession } from "next-auth/next";
import { errorhandler } from "@/lib/errorHandler";
@@ -67,6 +68,7 @@ export default async function handle(
id: undefined,
slug: linkData.slug ? linkData.slug + "-copy" : null,
name: newLinkName,
+ watermarkConfig: linkData.watermarkConfig || Prisma.JsonNull,
createdAt: undefined,
updatedAt: undefined,
},
diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.ts
index 243ba7a8..398b1bcd 100644
--- a/pages/api/links/[id]/index.ts
+++ b/pages/api/links/[id]/index.ts
@@ -52,6 +52,8 @@ export default async function handle(
enableAgreement: true,
agreement: true,
showBanner: true,
+ enableWatermark: true,
+ watermarkConfig: true,
},
});
@@ -211,6 +213,8 @@ export default async function handle(
enableAgreement: linkData.enableAgreement,
agreementId: linkData.agreementId || null,
showBanner: linkData.showBanner,
+ enableWatermark: linkData.enableWatermark || false,
+ watermarkConfig: linkData.watermarkConfig || null,
},
include: {
views: {
diff --git a/pages/api/links/domains/[...domainSlug].ts b/pages/api/links/domains/[...domainSlug].ts
index 64fcf75c..b73b4364 100644
--- a/pages/api/links/domains/[...domainSlug].ts
+++ b/pages/api/links/domains/[...domainSlug].ts
@@ -62,6 +62,8 @@ export default async function handle(
enableAgreement: true,
agreement: true,
showBanner: true,
+ enableWatermark: true,
+ watermarkConfig: true,
document: {
select: {
team: {
diff --git a/pages/api/links/download/index.ts b/pages/api/links/download/index.ts
index 2c6374f5..8c71b098 100644
--- a/pages/api/links/download/index.ts
+++ b/pages/api/links/download/index.ts
@@ -2,6 +2,7 @@ import { NextApiRequest, NextApiResponse } from "next";
import { getFile } from "@/lib/files/get-file";
import prisma from "@/lib/prisma";
+import { getIpAddress } from "@/lib/utils/ip";
export default async function handle(
req: NextApiRequest,
@@ -20,11 +21,15 @@ export default async function handle(
select: {
id: true,
viewedAt: true,
+ viewerEmail: true,
link: {
select: {
allowDownload: true,
expiresAt: true,
isArchived: true,
+ enableWatermark: true,
+ watermarkConfig: true,
+ name: true,
},
},
document: {
@@ -36,6 +41,7 @@ export default async function handle(
type: true,
file: true,
storageType: true,
+ numPages: true,
},
take: 1,
},
@@ -98,6 +104,46 @@ export default async function handle(
isDownload: true,
});
+ if (view.link.enableWatermark) {
+ const response = await fetch(
+ `${process.env.NEXTAUTH_URL}/api/mupdf/annotate-document`,
+ {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ url: downloadUrl,
+ numPages: view.document!.versions[0].numPages,
+ watermarkConfig: view.link.watermarkConfig,
+ viewerData: {
+ email: view.viewerEmail,
+ date: new Date(view.viewedAt).toLocaleDateString(),
+ ipAddress: getIpAddress(req.headers),
+ link: view.link.name,
+ time: new Date(view.viewedAt).toLocaleTimeString(),
+ },
+ }),
+ },
+ );
+
+ if (!response.ok) {
+ return res.status(500).json({ error: "Error downloading" });
+ }
+
+ const pdfBuffer = await response.arrayBuffer();
+
+ // Set appropriate headers
+ res.setHeader("Content-Type", "application/pdf");
+ res.setHeader(
+ "Content-Disposition",
+ 'attachment; filename="watermarked.pdf"',
+ );
+
+ // Send the buffer directly
+ return res.send(Buffer.from(pdfBuffer));
+ }
+
return res.status(200).json({ downloadUrl });
} catch (error) {
return res.status(500).json({
diff --git a/pages/api/links/index.ts b/pages/api/links/index.ts
index 1d86c9d6..dfd40c4b 100644
--- a/pages/api/links/index.ts
+++ b/pages/api/links/index.ts
@@ -160,6 +160,10 @@ export default async function handler(
enableAgreement: linkData.enableAgreement,
agreementId: linkData.agreementId,
}),
+ ...(linkData.enableWatermark && {
+ enableWatermark: linkData.enableWatermark,
+ watermarkConfig: linkData.watermarkConfig,
+ }),
showBanner: linkData.showBanner,
},
});
diff --git a/pages/api/mupdf/annotate-document.ts b/pages/api/mupdf/annotate-document.ts
new file mode 100644
index 00000000..c8da649f
--- /dev/null
+++ b/pages/api/mupdf/annotate-document.ts
@@ -0,0 +1,209 @@
+import { NextApiRequest, NextApiResponse } from "next";
+
+import fontkit from "@pdf-lib/fontkit";
+import Handlebars from "handlebars";
+import { PDFDocument, StandardFonts, degrees, rgb } from "pdf-lib";
+
+import { hexToRgb, log } from "@/lib/utils";
+
+// This function can run for a maximum of 120 seconds
+export const config = {
+ maxDuration: 180,
+};
+
+interface WatermarkConfig {
+ text: string;
+ isTiled: boolean;
+ position:
+ | "top-left"
+ | "top-center"
+ | "top-right"
+ | "middle-left"
+ | "middle-center"
+ | "middle-right"
+ | "bottom-left"
+ | "bottom-center"
+ | "bottom-right";
+ rotation: 0 | 30 | 45 | 90 | 180;
+ color: string;
+ fontSize: number;
+ opacity: number; // 0 to 0.8
+}
+
+interface ViewerData {
+ email: string;
+ date: string;
+ ipAddress: string;
+ link: string;
+ time: string;
+}
+
+function getPositionCoordinates(
+ position: WatermarkConfig["position"],
+ width: number,
+ height: number,
+ textWidth: number,
+ textHeight: number,
+): number[] {
+ const positions = {
+ "top-left": [10, height - textHeight],
+ "top-center": [(width - textWidth) / 2, height - textHeight],
+ "top-right": [width - textWidth - 10, height - textHeight],
+ "middle-left": [10, (height - textHeight) / 2],
+ "middle-center": [(width - textWidth) / 2, (height - textHeight) / 2],
+ "middle-right": [width - textWidth - 10, (height - textHeight) / 2],
+ "bottom-left": [10, 20],
+ "bottom-center": [(width - textWidth) / 2, 20],
+ "bottom-right": [width - textWidth - 10, 20],
+ };
+ return positions[position];
+}
+
+async function insertWatermark(
+ pdfDoc: PDFDocument,
+ config: WatermarkConfig,
+ viewerData: ViewerData,
+ pageIndex: number,
+): Promise<void> {
+ const pages = pdfDoc.getPages();
+ const page = pages[pageIndex];
+ const { width, height } = page.getSize();
+
+ const font = await pdfDoc.embedFont(StandardFonts.Helvetica);
+
+ // Compile the Handlebars template
+ const template = Handlebars.compile(config.text);
+ const watermarkText = template(viewerData);
+
+ // Calculate a responsive font size
+ const calculateFontSize = () => {
+ const baseFontSize = Math.min(width, height) * (config.fontSize / 1000);
+ return Math.max(8, Math.min(baseFontSize, config.fontSize));
+ };
+ const fontSize = calculateFontSize();
+
+ const textWidth = font.widthOfTextAtSize(watermarkText, fontSize);
+ const textHeight = font.heightAtSize(fontSize);
+
+ if (config.isTiled) {
+ const patternWidth = textWidth / 1.1;
+ const patternHeight = textHeight * 15;
+
+ // Calculate the offset to center the pattern
+ const offsetX = -patternWidth / 4;
+ const offsetY = -patternHeight / 4;
+
+ const maxTilesPerRow = Math.ceil(width / patternWidth) + 1;
+ const maxTilesPerColumn = Math.ceil(height / patternHeight) + 1;
+
+ for (let i = 0; i < maxTilesPerRow; i++) {
+ for (let j = 0; j < maxTilesPerColumn; j++) {
+ const x = i * patternWidth + offsetX;
+ const y = j * patternHeight + offsetY;
+
+ page.drawText(watermarkText, {
+ x,
+ y,
+ size: fontSize,
+ font,
+ color: hexToRgb(config.color) ?? rgb(0, 0, 0),
+ opacity: config.opacity,
+ rotate: degrees(config.rotation),
+ });
+ }
+ }
+ } else {
+ const [x, y] = getPositionCoordinates(
+ config.position,
+ width,
+ height,
+ textWidth,
+ textHeight,
+ );
+
+ page.drawText(watermarkText, {
+ x,
+ y,
+ size: fontSize,
+ font,
+ color: hexToRgb(config.color) ?? rgb(0, 0, 0),
+ opacity: config.opacity,
+ rotate: degrees(config.rotation),
+ });
+ }
+}
+
+export default async (req: NextApiRequest, res: NextApiResponse) => {
+ // check if post method
+ if (req.method !== "POST") {
+ res.status(405).json({ error: "Method Not Allowed" });
+ return;
+ }
+
+ // // Extract the API Key from the Authorization header
+ // const authHeader = req.headers.authorization;
+ // const token = authHeader?.split(" ")[1]; // Assuming the format is "Bearer [token]"
+
+ // // Check if the API Key matches
+ // if (token !== process.env.INTERNAL_API_KEY) {
+ // res.status(401).json({ message: "Unauthorized" });
+ // return;
+ // }
+
+ const { url, watermarkConfig, viewerData, numPages } = req.body as {
+ url: string;
+ watermarkConfig: WatermarkConfig;
+ viewerData: ViewerData;
+ numPages: number;
+ };
+
+ try {
+ // Fetch the PDF data
+ let response: Response;
+ try {
+ response = await fetch(url);
+ } catch (error) {
+ log({
+ message: `Failed to fetch PDF in conversion process with error: \n\n Error: ${error}`,
+ type: "error",
+ mention: true,
+ });
+ throw new Error(`Failed to fetch pdf`);
+ }
+
+ // Convert the response to a buffer
+ const pdfBuffer = await response.arrayBuffer();
+
+ // Load the PDF document
+ const pdfDoc = await PDFDocument.load(pdfBuffer);
+
+ // Register fontkit
+ pdfDoc.registerFontkit(fontkit);
+
+ // Add watermark to each page
+ for (let i = 0; i < numPages; i++) {
+ await insertWatermark(pdfDoc, watermarkConfig, viewerData, i);
+ }
+
+ // Save the modified PDF
+ const pdfBytes = await pdfDoc.save();
+
+ // Set appropriate headers
+ res.setHeader("Content-Type", "application/pdf");
+ res.setHeader(
+ "Content-Disposition",
+ 'attachment; filename="watermarked.pdf"',
+ );
+
+ res.status(200).send(Buffer.from(pdfBytes));
+
+ return;
+ } catch (error) {
+ log({
+ message: `Failed to convert page with error: \n\n Error: ${error}`,
+ type: "error",
+ mention: true,
+ });
+ throw error;
+ }
+};
diff --git a/pages/api/views-dataroom.ts b/pages/api/views-dataroom.ts
index 015e8e1c..f23ada27 100644
--- a/pages/api/views-dataroom.ts
+++ b/pages/api/views-dataroom.ts
@@ -3,7 +3,6 @@ import { NextApiRequest, NextApiResponse } from "next";
import { waitUntil } from "@vercel/functions";
import { getServerSession } from "next-auth/next";
import { parsePageId } from "notion-utils";
-import { record } from "zod";
import sendNotification from "@/lib/api/notification-helper";
import { sendVerificationEmail } from "@/lib/emails/send-email-verification";
@@ -12,8 +11,9 @@ import { newId } from "@/lib/id-helper";
import notion from "@/lib/notion";
import prisma from "@/lib/prisma";
import { parseSheet } from "@/lib/sheet";
-import { CustomUser } from "@/lib/types";
+import { CustomUser, WatermarkConfigSchema } from "@/lib/types";
import { checkPassword, decryptEncrpytedPassword, log } from "@/lib/utils";
+import { getIpAddress } from "@/lib/utils/ip";
import { authOptions } from "./auth/[...nextauth]";
@@ -99,6 +99,8 @@ export default async function handle(
denyList: true,
enableAgreement: true,
agreementId: true,
+ enableWatermark: true,
+ watermarkConfig: true,
},
});
@@ -518,6 +520,15 @@ export default async function handle(
: recordMap
? "notion"
: undefined,
+ watermarkConfig: link.enableWatermark ? link.watermarkConfig : undefined,
+ ipAddress:
+ link.enableWatermark &&
+ link.watermarkConfig &&
+ WatermarkConfigSchema.parse(link.watermarkConfig).text.includes(
+ "{{ipAddress}}",
+ )
+ ? getIpAddress(req.headers)
+ : undefined,
};
return res.status(200).json(returnObject);
diff --git a/pages/api/views.ts b/pages/api/views.ts
index 43a90a07..f29d15a8 100644
--- a/pages/api/views.ts
+++ b/pages/api/views.ts
@@ -9,8 +9,9 @@ import { getFile } from "@/lib/files/get-file";
import { newId } from "@/lib/id-helper";
import prisma from "@/lib/prisma";
import { parseSheet } from "@/lib/sheet";
-import { CustomUser } from "@/lib/types";
+import { CustomUser, WatermarkConfigSchema } from "@/lib/types";
import { checkPassword, decryptEncrpytedPassword, log } from "@/lib/utils";
+import { getIpAddress } from "@/lib/utils/ip";
import { authOptions } from "./auth/[...nextauth]";
@@ -86,6 +87,8 @@ export default async function handle(
denyList: true,
enableAgreement: true,
agreementId: true,
+ enableWatermark: true,
+ watermarkConfig: true,
},
});
@@ -400,6 +403,15 @@ export default async function handle(
: documentPages
? "pdf"
: undefined,
+ watermarkConfig: link.enableWatermark ? link.watermarkConfig : undefined,
+ ipAddress:
+ link.enableWatermark &&
+ link.watermarkConfig &&
+ WatermarkConfigSchema.parse(link.watermarkConfig).text.includes(
+ "{{ipAddress}}",
+ )
+ ? getIpAddress(req.headers)
+ : undefined,
};
return res.status(200).json(returnObject);
diff --git a/prisma/migrations/20240830000000_add_watermarks/migration.sql b/prisma/migrations/20240830000000_add_watermarks/migration.sql
new file mode 100644
index 00000000..a421fb72
--- /dev/null
+++ b/prisma/migrations/20240830000000_add_watermarks/migration.sql
@@ -0,0 +1,4 @@
+-- AlterTable
+ALTER TABLE "Link" ADD COLUMN "enableWatermark" BOOLEAN DEFAULT false,
+ADD COLUMN "watermarkConfig" JSONB;
+
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index f40fda75..e125b1bc 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -231,6 +231,8 @@ model Link {
agreement Agreement? @relation(fields: [agreementId], references: [id], onDelete: SetNull)
agreementId String? // This can be nullable, representing links without agreements
showBanner Boolean? @default(false) // Optional give user a option to show the banner and end of document signup form
+ enableWatermark Boolean? @default(false) // Optional give user a option to enable the watermark
+ watermarkConfig Json? // This will store the watermark configuration: {text: "Confidential", isTiled: false, color: "#000000", fontSize: 12, opacity: 0.5, rotation: 30, position: "top-right"}
// custom metatags
metaTitle String? // This will be the meta title of the link