Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 'survey-pdf' fails on Node.js #289

Merged
merged 7 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/flat_layout/flat_html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FlatQuestion } from './flat_question';
import { FlatRepository } from './flat_repository';
import { IPdfBrick } from '../pdf_render/pdf_brick';
import { SurveyHelper } from '../helper_survey';
import { replace } from 'lodash';
import { EmptyBrick } from '../pdf_render/pdf_empty';

export type IHTMLRenderType = 'auto' | 'standard' | 'image';
export class FlatHTML extends FlatQuestion {
Expand Down Expand Up @@ -34,6 +34,9 @@ export class FlatHTML extends FlatQuestion {
}
public async generateFlatsContent(point: IPoint): Promise<IPdfBrick[]> {
let renderAs: IHTMLRenderType = <IHTMLRenderType>this.question.renderAs;
if(!SurveyHelper.hasDocument) {
return [new EmptyBrick(SurveyHelper.createRect(point, 0, 0))];
}
if (renderAs === 'auto') renderAs = this.controller.htmlRenderAs;
if (renderAs === 'auto') renderAs = this.chooseRender(SurveyHelper.getLocString(this.question.locHtml));
const html: string = SurveyHelper.createHtmlContainerBlock(SurveyHelper.getLocString(this.question.locHtml), this.controller, renderAs);
Expand Down
17 changes: 12 additions & 5 deletions src/flat_layout/flat_signaturepad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FlatRepository } from './flat_repository';
import { IPoint, DocController } from '../doc_controller';
import { IPdfBrick } from '../pdf_render/pdf_brick';
import { SurveyHelper } from '../helper_survey';
import { EmptyBrick } from '../pdf_render/pdf_empty';
import { CompositeBrick } from '../pdf_render/pdf_composite';

export class FlatSignaturePad extends FlatQuestion {
Expand All @@ -19,11 +20,17 @@ export class FlatSignaturePad extends FlatQuestion {
return await SurveyHelper.createImageFlat(point, this.question, this.controller, { link: this.question.backgroundImage, width: SurveyHelper.pxToPt(<any>this.question.signatureWidth), height: SurveyHelper.pxToPt(<any>this.question.signatureHeight), objectFit: 'cover' }, true);
}
public async generateSign(point: IPoint): Promise<IPdfBrick> {
return await SurveyHelper.createImageFlat(point,
this.question, this.controller, { link: this.question.value,
width: SurveyHelper.pxToPt(<any>this.question.signatureWidth),
height: SurveyHelper.pxToPt(<any>this.question.signatureHeight) }, false
);
const width = SurveyHelper.pxToPt(<any>this.question.signatureWidth);
const height = SurveyHelper.pxToPt(<any>this.question.signatureHeight);
if(this.question.value) {
return await SurveyHelper.createImageFlat(point,
this.question, this.controller, { link: this.question.value,
width: width,
height: height }, false
);
} else {
return new EmptyBrick(SurveyHelper.createRect(point, width, height));
}
}

public async generateFlatsContent(point: IPoint): Promise<IPdfBrick[]> {
Expand Down
5 changes: 4 additions & 1 deletion src/helper_survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ export class SurveyHelper {
}
public static inBrowser = typeof Image === 'function';

public static get hasDocument(): boolean {
return typeof document !== 'undefined';
}

public static async getImageBase64(imageLink: string): Promise<string> {
const image = new Image();
image.crossOrigin='anonymous';
Expand Down Expand Up @@ -497,7 +501,6 @@ export class SurveyHelper {
const html: string = `<img src='${await SurveyHelper.getImageLink(controller, imageOptions, applyImageFit ?? controller.applyImageFit)}' width='${imageOptions.width}' height='${imageOptions.height}'/>`;
return new HTMLBrick(question, controller, this.createRect(point, imageOptions.width, imageOptions.height), html, true);
}

return new ImageBrick(question, controller, imageOptions.link, point, imageOptions.width, imageOptions.height);
}
public static canPreviewImage(question: QuestionFileModel, item: { name: string, type: string, content: string }, url: string): boolean {
Expand Down
7 changes: 5 additions & 2 deletions src/pdf_render/pdf_image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ export class ImageBrick extends PdfBrick {
}
public async renderInteractive(): Promise<void> {
await new Promise<void>((resolve) => {
this.controller.doc.addImage(this.image, this.xLeft, this.yTop, this.originalWidth, this.originalHeight);
resolve();
try {
this.controller.doc.addImage(this.image, 'PNG', this.xLeft, this.yTop, this.originalWidth, this.originalHeight);
} finally {
resolve();
}
});
}
}
83 changes: 83 additions & 0 deletions tests/node.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

/**
* @jest-environment node
*/
import { SurveyPDF } from '../src/survey';
import { DocController, IRect } from '../src/doc_controller';
import { FlatSurvey } from '../src/flat_layout/flat_survey';
import { IPdfBrick } from '../src/pdf_render/pdf_brick';
import { TestHelper } from '../src/helper_test';
import { EmptyBrick } from '../src/pdf_render/pdf_empty';
import { SurveyHelper } from '../src/helper_survey';
import { CompositeBrick } from '../src/pdf_render/pdf_composite';
import { ImageBrick } from '../src/pdf_render/pdf_image';

test('Check html brick is empty when document is not defined', async () => {
let json: any = {
elements: [
{
type: 'html',
name: 'html_chooserender_standard',
html: '<b>STRONG POWER</b>'
},
]
};
let survey: SurveyPDF = new SurveyPDF(json, TestHelper.defaultOptions);
let controller: DocController = new DocController(TestHelper.defaultOptions);
let flats: IPdfBrick[][] = await FlatSurvey.generateFlats(survey, controller);
expect(flats.length).toBe(1);
expect(flats[0].length).toBe(1);
const emptyBrick = flats[0][0].unfold()[0];
expect(emptyBrick instanceof EmptyBrick).toBe(true);
expect(emptyBrick.height).toEqual(0);
expect(emptyBrick.width).toEqual(0);
});

test('Check signaturepad with empty value', async () => {
let json: any = {
questions: [
{
type: 'signaturepad',
name: 'sigpadque',
titleLocation: 'hidden',
}
]
};
let survey: SurveyPDF = new SurveyPDF(json, TestHelper.defaultOptions);
let controller: DocController = new DocController(TestHelper.defaultOptions);
let flats: IPdfBrick[][] = await FlatSurvey.generateFlats(survey, controller);
expect(flats.length).toBe(1);
expect(flats[0].length).toBe(1);
expect(flats[0][0] instanceof CompositeBrick).toBeTruthy();
expect((<CompositeBrick>flats[0][0])['bricks'].length).toBe(1);
const htmlBrick = (<CompositeBrick>flats[0][0])['bricks'][0];
expect(htmlBrick instanceof EmptyBrick).toBeTruthy();
let assumeHTML: IRect = {
xLeft: controller.leftTopPoint.xLeft + controller.unitWidth,
xRight: controller.leftTopPoint.xLeft + controller.unitWidth +
SurveyHelper.pxToPt((<any>survey.getAllQuestions()[0]).signatureWidth),
yTop: controller.leftTopPoint.yTop,
yBot: controller.leftTopPoint.yTop +
SurveyHelper.pxToPt((<any>survey.getAllQuestions()[0]).signatureHeight)
};
TestHelper.equalRect(expect, htmlBrick, assumeHTML);
});

test('Check image with non base64 link', async () => {
const json: any = {
elements: [
{
type: 'image',
name: 'image_question',
titleLocation: 'hidden',
imageLink: 'https://surveyjs.io/Content/Images/examples/image-picker/lion.jpg'
}
]
};
const survey: SurveyPDF = new SurveyPDF(json, TestHelper.defaultOptions);
try {
await survey.raw();
} catch {
throw(new Error('Could not export pdf'));
}
});
Loading