Skip to content

Commit e9f0a0b

Browse files
authored
Merge pull request #34 from surveyjs/nextjs-form-builder-upd-savePdf-action
Add a PDF export utility
2 parents 03e6b67 + aac5822 commit e9f0a0b

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

blogpost-apps/nextjs-form-builder/src/components/FormBuilder.tsx

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import {
1111
SurveyCreatorComponent,
1212
SurveyCreator
1313
} from 'survey-creator-react';
14-
import { ComputedUpdater, Action, SurveyModel, SvgRegistry } from 'survey-core';
15-
import { IDocOptions, SurveyPDF } from 'survey-pdf';
1614
import 'survey-core/survey-core.css';
1715
import 'survey-creator-core/survey-creator-core.css';
1816
import 'ace-builds/src-noconflict/ace';
1917
import 'ace-builds/src-noconflict/ext-searchbox';
2018
import SurveyCreatorTheme from 'survey-creator-core/themes';
2119
import { creatorTheme } from '../styles/form-builder-theme';
20+
import { createPdfAction } from '../utils/surveyPdf';
21+
import { Action } from 'survey-core';
2222

2323
registerCreatorTheme(SurveyCreatorTheme);
2424

@@ -57,37 +57,7 @@ export default function FormBuilderComponent(props: {
5757

5858
creator.applyCreatorTheme(creatorTheme);
5959

60-
function savePdf(survey: SurveyModel) {
61-
const pdfDocOptions: IDocOptions = {
62-
fontSize: 14,
63-
margins: {
64-
left: 10,
65-
right: 10,
66-
top: 10,
67-
bot: 10
68-
},
69-
format: [210, 297]
70-
};
71-
const surveyPDF = new SurveyPDF(survey.toJSON(), pdfDocOptions);
72-
surveyPDF.readOnly = survey.pdfReadonly;
73-
surveyPDF.locale = survey.locale;
74-
surveyPDF.data = survey.data;
75-
surveyPDF.save(survey.pdfFileName);
76-
}
77-
const customIcon = '<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M6 2a2 2 0 0 0-2 2v16c0 1.1.9 2 2 2h12a2 2 0 0 0 2-2V8l-6-6H6zm7 1.5L18.5 9H13V3.5zM12 13v4.17l-1.59-1.58L9 17l4 4 4-4-1.41-1.41L13 17.17V13h-1z"/></svg>';
78-
SvgRegistry.registerIcon("icon-savepdf", customIcon);
79-
const savePdfAction = new Action({
80-
id: 'svd-save-pdf',
81-
title: 'Save as PDF',
82-
iconName: 'icon-savepdf',
83-
visible: new ComputedUpdater(() => creator.activeTab === 'preview'),
84-
enabled: true,
85-
action: () => {
86-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
87-
const surveyModel = (creator.getPlugin("preview") as any).model.survey as SurveyModel;
88-
savePdf(surveyModel);
89-
}
90-
});
60+
const savePdfAction: Action = createPdfAction(creator);
9161
creator.toolbar.actions.push(savePdfAction);
9262
creator.footerToolbar.actions.push(savePdfAction);
9363

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { SurveyModel, SvgRegistry, Action, ComputedUpdater } from "survey-core";
2+
import { SurveyPDF, IDocOptions } from "survey-pdf";
3+
import { SurveyCreator } from 'survey-creator-react';
4+
5+
function savePdf(survey: SurveyModel) {
6+
const pdfDocOptions: IDocOptions = {
7+
fontSize: 14,
8+
margins: {
9+
left: 10,
10+
right: 10,
11+
top: 10,
12+
bot: 10
13+
},
14+
format: [210, 297]
15+
};
16+
const surveyPDF = new SurveyPDF(survey.toJSON(), pdfDocOptions);
17+
surveyPDF.readOnly = survey.pdfReadonly;
18+
surveyPDF.locale = survey.locale;
19+
surveyPDF.data = survey.data;
20+
surveyPDF.save(survey.pdfFileName);
21+
}
22+
export function createPdfAction(creator: SurveyCreator): Action {
23+
const customIcon = '<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M6 2a2 2 0 0 0-2 2v16c0 1.1.9 2 2 2h12a2 2 0 0 0 2-2V8l-6-6H6zm7 1.5L18.5 9H13V3.5zM12 13v4.17l-1.59-1.58L9 17l4 4 4-4-1.41-1.41L13 17.17V13h-1z"/></svg>';
24+
SvgRegistry.registerIcon("icon-savepdf", customIcon);
25+
26+
const savePdfAction = new Action({
27+
id: 'svd-save-pdf',
28+
title: 'Save as PDF',
29+
iconName: 'icon-savepdf',
30+
visible: new ComputedUpdater(() => creator.activeTab === 'preview'),
31+
enabled: true,
32+
action: () => {
33+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
34+
const surveyModel = (creator.getPlugin("preview") as any).model.survey as SurveyModel;
35+
savePdf(surveyModel);
36+
}
37+
});
38+
return savePdfAction;
39+
}

0 commit comments

Comments
 (0)