survey-pdf
Version:
A UI component that uses SurveyJS form JSON schemas to render forms as PDF documents. It populates PDF fields with data collected using SurveyJS Form Library and lets you export your SurveyJS forms as editable or pre-filled PDFs.
314 lines (313 loc) • 11.5 kB
TypeScript
import { IHTMLRenderType } from './flat_layout/flat_html';
import { LocalizableString } from 'survey-core';
import './jspdf_plugins/acroform.js';
import './jspdf_plugins/from_html.js';
export interface IPoint {
/**
* An X-coordinate for the left element edge.
*/
xLeft: number;
/**
* A Y-coordinate for the top element edge.
*/
yTop: number;
}
/**
* An interface that describes a rectangle.
*/
export interface IRect extends IPoint {
/**
* An X-coordinate for the right element edge.
*/
xRight: number;
/**
* A Y-coordinate for the bottom element edge.
*/
yBot: number;
}
export interface ISize {
width: number;
height: number;
}
export interface IMarginLR {
/**
* A left margin.
*/
left?: number;
/**
* A right margin.
*/
right?: number;
}
/**
* An interface that describes margins.
*/
export interface IMargin extends IMarginLR {
/**
* A top margin.
*/
top?: number;
/**
* A bottom margin.
*/
bot?: number;
}
/**
* PDF document configuration. Pass it as the second argument to the `SurveyPDF` constructor:
*
* ```js
* const surveyPdf = new SurveyPDF.SurveyPDF(surveyJson, pdfDocOptions);
*
* // In modular applications:
* import { SurveyPDF } from "survey-pdf";
* const surveyPdf = new SurveyPDF(surveyJson, pdfDocOptions);
* ```
*/
export interface IDocOptions {
/**
* Page orientation.
*
* Possible values:
*
* - `"p"` (default) - Portrait orientation.
* - `"l"` - Landscape orientation.
*
* @see format
*/
orientation?: 'p' | 'l';
/**
* Page format.
*
* Possible values:
*
* - `"a0"` - `"a10"` (`"a4"` is default)
* - `"b0"` - `"b10"`
* - `"c0"` - `"c10"`
* - `"dl"`
* - `"letter"`
* - `"government-letter"`
* - `"legal"`
* - `"junior-legal"`
* - `"ledger"`
* - `"tabloid"`
* - `"credit-card"`
* - `[width, height]` - Custom page size in millimeters, for example, `[210, 297]`.
*
* [View Demo](https://surveyjs.io/pdf-generator/examples/save-completed-forms-as-pdf-files/ (linkStyle))
* @see orientation
*/
format?: string | number[];
/**
* Font size in points.
*
* Default value: 14
*
* @see fontName
*/
fontSize?: number;
/**
* Font name.
*
* Possible values:
*
* - `"Helvetica"` (default)
* - `"Courier"`
* - `"Times"`
* - `"Symbol"`
* - `"ZapfDingbats"`
* - [Custom font name](https://surveyjs.io/pdf-generator/documentation/customize-pdf-form-settings#custom-fonts)
*
* [View Demo](https://surveyjs.io/pdf-generator/examples/change-font-in-pdf-form/ (linkStyle))
* @see fontSize
*/
fontName?: string;
base64Normal?: string;
base64Bold?: string;
/**
* Specifies whether to apply a custom font to [HTML questions](https://surveyjs.io/form-library/examples/questiontype-html/).
*
* Default value: `false`
*
* [View Demo](https://surveyjs.io/pdf-generator/examples/change-font-in-pdf-form/ (linkStyle))
* @see htmlRenderAs
*/
useCustomFontInHtml?: boolean;
/**
* Page margins.
*
* Set this property to an object with the following fields: `top`, `bot`, `left`, `right`.
*
* [View Demo](https://surveyjs.io/pdf-generator/examples/save-completed-forms-as-pdf-files/ (linkStyle))
*/
margins?: IMargin;
/**
* Specifies how to render [HTML questions](https://surveyjs.io/Documentation/Library?id=questionhtmlmodel) into PDF.
*
* Possible values:
*
* - `"standard"` - Render HTML questions as selectable text.
* - `"image"` - Render HTML questions as images.
* - `"auto"` (default) - Select between the `"standard"` and `"image"` modes automatically based on the HTML content.
*
* [View Demo](https://surveyjs.io/pdf-generator/examples/split-html-image-across-pages/ (linkStyle))
*
* You can override this property for an individual HTML question. Set the question's `renderAs` property to `"standard"` or `"image"` in the survey JSON schema.
* @see useCustomFontInHtml
*/
htmlRenderAs?: IHTMLRenderType;
/**
* Specifies how to render [Matrix](https://surveyjs.io/Documentation/Library?id=questionmatrixmodel), [Matrix Dropdown](https://surveyjs.io/Documentation/Library?id=questionmatrixdropdownmodel), and [Matrix Dynamic](https://surveyjs.io/Documentation/Library?id=questionmatrixdynamicmodel) questions into PDF.
*
* Possible values:
*
* - `"list"` - Render matrix-like questions as lists.
* - `"auto"` (default) - Render matrix-like questions as tables if they fit into the available space. Otherwise, render the questions as lists.
*
* You can override this property for an individual matrix-like question. Set the question's `renderAs` property to `"list"` in the survey JSON schema.
*/
matrixRenderAs?: 'auto' | 'list';
useLegacyBooleanRendering?: boolean;
/**
* Specifies how to render read-only questions.
*
* Possible values:
*
* - `"text"` - Render read-only questions as plain text and custom primitives.
* - `"acroform"` - Use Adobe AcroForms to render questions that support them as interactive form elements switched to their native read-only state. Other questions are rendered in `"text"` mode.
* - `"auto"` (default) - Prefer the `"text"` mode but use `"acroform"` for [File Upload](https://surveyjs.io/form-library/documentation/api-reference/file-model) questions and links.
*/
readonlyRenderAs?: 'auto' | 'text' | 'acroform';
textFieldRenderAs?: 'singleLine' | 'multiLine';
/**
* Specifies whether to compress the PDF document. Compressed documents do not support [custom fonts](https://surveyjs.io/Documentation/Pdf-Export?id=Customization-ChangeFonts#use-custom-font).
*
* Default value: `false`
*/
compress?: boolean;
/**
* Specifies whether to apply `imageFit` settings specified in the survey JSON schema to exported images.
*
* Default value: `false`
*
* This property applies the following settings:
*
* - [`imageFit`](https://surveyjs.io/form-library/documentation/api-reference/add-image-to-survey#imageFit) to exported [Image](https://surveyjs.io/form-library/documentation/api-reference/add-image-to-survey) questions
* - [`imageFit`](https://surveyjs.io/form-library/documentation/api-reference/image-picker-question-model#imageFit) to exported [Image Picker](https://surveyjs.io/form-library/documentation/api-reference/image-picker-question-model) questions
* - [`logoFit`](https://surveyjs.io/form-library/documentation/api-reference/survey-data-model#logoFit) to an exported logo in the survey header
*
* If you enable the `applyImageFit` property, the quality of images may be lower because they pass through several conversions. If `applyImageFit` is disabled, exported images fill the entire container and do not preserve their aspect ratio, but their quality remains the same because they are exported as is.
*/
applyImageFit?: boolean;
/**
* Specifies whether the PDF document contains text in right-to-left languages.
*
* Default value: `false`
*/
isRTL?: boolean;
/**
* Specifies whether to include only selected choices when PDF Generator renders a [Multi-Select Dropdown (Tag Box)](https://surveyjs.io/form-library/examples/how-to-create-multiselect-tag-box/) question.
*
* Default value: `false` (include all choices)
*/
tagboxSelectedChoicesOnly?: boolean;
}
export declare class DocOptions implements IDocOptions {
static readonly MM_TO_PT: number;
static readonly FONT_SIZE = 14;
protected _orientation: 'l' | 'p';
protected _format: string | number[];
protected _fontSize: number;
protected _fontName: string;
static SEGOE_NORMAL: string;
static SEGOE_BOLD: string;
protected _base64Normal: string;
protected _base64Bold: string;
protected _useCustomFontInHtml: boolean;
protected _margins: IMargin;
protected _htmlRenderAs: IHTMLRenderType;
protected _matrixRenderAs: 'auto' | 'list';
protected _readonlyRenderAs: 'auto' | 'text' | 'acroform';
protected _compress: boolean;
protected _applyImageFit: boolean;
protected _useLegacyBooleanRendering: boolean;
protected _isRTL: boolean;
protected _tagboxSelectedChoicesOnly: boolean;
constructor(options: IDocOptions);
get leftTopPoint(): IPoint;
get fontSize(): number;
get fontName(): string;
get base64Normal(): string;
get base64Bold(): string;
get useCustomFontInHtml(): boolean;
get margins(): IMargin;
get format(): string | number[];
get orientation(): 'l' | 'p';
get htmlRenderAs(): IHTMLRenderType;
get matrixRenderAs(): 'auto' | 'list';
get readonlyRenderAs(): 'auto' | 'text' | 'acroform';
get compress(): boolean;
get applyImageFit(): boolean;
get useLegacyBooleanRendering(): boolean;
get isRTL(): boolean;
get tagboxSelectedChoicesOnly(): boolean;
}
/**
* The `DocController` object includes an API that allows you to configure main PDF document properties (font, margins, page width and height).
*
* [View Demo](https://surveyjs.io/pdf-generator/examples/change-font-in-pdf-form/ (linkStyle))
*/
export declare class DocController extends DocOptions {
private _doc;
private _helperDoc;
private _fontStyle;
private marginsStack;
constructor(options?: IDocOptions);
static customFonts: {
[name: string]: {
normal: string;
bold: string;
italic: string;
bolditalic: string;
};
};
/**
* Adds a custom font to the PDF Generator.
*
* [View Demo](https://surveyjs.io/pdf-generator/examples/change-font-in-pdf-form/ (linkStyle))
* @param fontName A custom name that you will use to apply the custom font.
* @param base64 The custom font as a Base64-encoded string. To encode your font to Base64, obtain it as a TTF file and use any TTF-to-Base64 online converter.
* @param fontStyle The style of the custom font: `"normal"`, `"bold"`, `"italic"`, or `"bolditalic"`.
*/
static addFont(fontName: string, base64: string, fontStyle: 'normal' | 'bold' | 'italic' | 'bolditalic'): void;
get doc(): any;
get helperDoc(): any;
get fontName(): string;
set fontName(fontName: string);
get fontSize(): number;
set fontSize(fontSize: number);
get fontStyle(): string;
set fontStyle(fontStyle: string);
measureText(text?: string | LocalizableString | number, fontStyle?: string, fontSize?: number): ISize;
/**
* The width of one character in pixels.
*/
get unitWidth(): number;
/**
* The heigth of one character in pixels.
*/
get unitHeight(): number;
pushMargins(left?: number, right?: number): void;
popMargins(): void;
/**
* The width of a PDF page in pixels.
*/
get paperWidth(): number;
/**
* The height of a PDF page in pixels.
*/
get paperHeight(): number;
getNumberOfPages(): number;
addPage(): void;
getCurrentPageIndex(): number;
setPage(index: number): void;
}