Skip to content

Commit a1751ae

Browse files
committed
feat: replace generation with pdfmake
1 parent e308219 commit a1751ae

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

frontend/src/features/admin-form/responses/IndividualResponsePage/IndividualResponseNavbar.tsx

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
useNavigate,
99
useParams,
1010
} from 'react-router-dom'
11-
import { useReactToPrint } from 'react-to-print'
1211
import {
1312
Box,
1413
ButtonGroup,
@@ -20,7 +19,6 @@ import {
2019
Stack,
2120
Text,
2221
} from '@chakra-ui/react'
23-
import { datadogLogs } from '@datadog/browser-logs'
2422
import { useFeatureIsOn, useGrowthBook } from '@growthbook/growthbook-react'
2523

2624
import { featureFlags } from '~shared/constants'
@@ -33,6 +31,7 @@ import { useUser } from '~features/user/queries'
3331

3432
import { useUnlockedResponses } from '../ResponsesPage/storage/UnlockedResponses/UnlockedResponsesProvider'
3533

34+
import generatePdf from './generatePdf'
3635
import PrintableResponse from './PrintableResponse'
3736
import { useIndividualSubmission } from './queries'
3837

@@ -131,10 +130,6 @@ export const IndividualResponseNavbar = (): JSX.Element => {
131130
const isAdminPrintPdfEnabled = useFeatureIsOn(featureFlags.adminPrintPdf)
132131

133132
const printableResponseRef = useRef<HTMLDivElement>(null)
134-
const reactToPrintFn = useReactToPrint({
135-
contentRef: printableResponseRef,
136-
documentTitle: `${form ? `${form.title}_formId_${form._id}_` : ''}submissionId_${submissionId}_response.pdf`,
137-
})
138133

139134
return (
140135
<Grid
@@ -175,19 +170,16 @@ export const IndividualResponseNavbar = (): JSX.Element => {
175170
aria-label="Print"
176171
icon={<FaRegFilePdf />}
177172
isLoading={isLoading || isFormLoading}
178-
onClick={() => {
179-
datadogLogs.logger.info(
180-
`IndividualResponseNavbar: admin printing pdf`,
181-
{
182-
meta: {
183-
action: 'adminPrintPdf',
184-
userId: user?._id,
185-
submissionId: submissionId,
186-
},
187-
},
188-
)
189-
reactToPrintFn()
190-
}}
173+
onClick={
174+
form
175+
? () =>
176+
generatePdf({
177+
formId: form._id,
178+
formTitle: form.title,
179+
submissionId,
180+
})
181+
: undefined
182+
}
191183
variant="clear"
192184
/>
193185
<PrintableResponse ref={printableResponseRef} />
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pdfMake from 'pdfmake/build/pdfmake'
2+
import pdfFonts from 'pdfmake/build/vfs_fonts'
3+
4+
pdfMake.vfs = pdfFonts.vfs
5+
6+
const docDefinition = { content: 'This is a sample PDF printed with pdfMake' }
7+
8+
interface ResponsePdfRenderData {
9+
formId: string
10+
formTitle: string
11+
submissionId: string
12+
}
13+
14+
const generatePdf = ({
15+
formId,
16+
formTitle,
17+
submissionId,
18+
}: ResponsePdfRenderData) => {
19+
const pdfTitle = `${formTitle}_formId_${formId}_submissionId_${submissionId}_response.pdf`
20+
21+
return pdfMake.createPdf(docDefinition).download(pdfTitle, () => {
22+
console.log('PDF downloaded')
23+
})
24+
}
25+
26+
export default generatePdf

0 commit comments

Comments
 (0)