2023-12-07 14:52:12 +00:00
|
|
|
import type { DocumentData } from '@documenso/prisma/client';
|
|
|
|
|
|
|
|
|
|
import { getFile } from '../universal/upload/get-file';
|
2024-02-21 02:19:35 +00:00
|
|
|
import { downloadFile } from './download-file';
|
2023-12-07 14:52:12 +00:00
|
|
|
|
|
|
|
|
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);
|
2023-12-07 14:52:12 +00:00
|
|
|
|
2024-01-02 04:38:35 +00:00
|
|
|
const blob = new Blob([bytes], {
|
|
|
|
|
type: 'application/pdf',
|
|
|
|
|
});
|
2023-12-07 14:52:12 +00:00
|
|
|
|
2024-03-05 23:05:32 +00:00
|
|
|
const baseTitle = (fileName ?? 'document').replace(/\.pdf$/, '');
|
2023-12-07 14:52:12 +00:00
|
|
|
|
2024-02-21 02:19:35 +00:00
|
|
|
downloadFile({
|
2024-03-05 23:05:32 +00:00
|
|
|
filename: `${baseTitle}.pdf`,
|
2024-02-21 02:19:35 +00:00
|
|
|
data: blob,
|
|
|
|
|
});
|
2023-12-07 14:52:12 +00:00
|
|
|
};
|