Files
sign/packages/lib/client-only/download-pdf.ts

30 lines
721 B
TypeScript
Raw Normal View History

import type { DocumentData } from '@documenso/prisma/client';
import { getFile } from '../universal/upload/get-file';
type DownloadPDFProps = {
documentData: DocumentData;
fileName?: string;
};
2024-01-02 04:38:35 +00:00
export const downloadPDF = async ({ documentData, fileName }: DownloadPDFProps) => {
const bytes = await getFile(documentData);
2024-01-02 04:38:35 +00:00
const blob = new Blob([bytes], {
type: 'application/pdf',
});
2024-01-02 04:38:35 +00:00
const link = window.document.createElement('a');
2024-01-18 04:23:22 +00:00
const [baseTitle] = fileName?.includes('.pdf')
? fileName.split('.pdf')
: [fileName ?? 'document'];
2024-01-02 04:38:35 +00:00
link.href = window.URL.createObjectURL(blob);
2024-01-18 04:23:22 +00:00
link.download = `${baseTitle}_signed.pdf`;
2024-01-02 04:38:35 +00:00
link.click();
2024-01-02 04:38:35 +00:00
window.URL.revokeObjectURL(link.href);
};