Skip to content

Commit

Permalink
Work for #376 - Signature Pad - Export a Signature to PDF instead of …
Browse files Browse the repository at this point in the history
…a question name (e.g., 'signature')
  • Loading branch information
tsv2013 committed Dec 4, 2023
1 parent 9bb6655 commit 7477ae2
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/tables/tabulator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ITableOptions, Table, TableRow } from "./table";
import { SurveyModel } from "survey-core";
import { Question, QuestionSignaturePadModel, SurveyModel } from "survey-core";
import { ColumnDataType, IColumnData, QuestionLocation } from "./config";
import { DocumentHelper } from "../utils";
import { localization } from "../localizationManager";
Expand Down Expand Up @@ -44,6 +44,33 @@ export const defaultDownloadOptions = {
cellWidth: 1,
},
margin: { top: 10, right: 10, bottom: 10, left: 10 },
didParseCell: function(data) {
if (data && data.cell.raw.content) {
const hasImagePrefix = (data.cell.raw.content || "").substring(0, 5) === "<img-";
if(hasImagePrefix) {
data.cell.height = 200;
}
}
},
willDrawCell: function(data) {
if (data && data.cell.raw.content) {
const hasImagePrefix = (data.cell.raw.content || "").substring(0, 5) === "<img-";
if(hasImagePrefix) {
const imagePreficIndex = data.cell.raw.content.indexOf(">");
const imageUrl = data.cell.raw.content.substring(imagePreficIndex+1);
if(!!imageUrl) {
const dims = data.cell.raw.content.substring(5, imagePreficIndex).split("-");
// var dim = data.cell.height - data.cell.padding("vertical");
doc.addImage(imageUrl, data.cell.x, data.cell.y, Number.parseInt(dims[0]), Number.parseInt(dims[1]));
data.cell.contentWidth = data.cell.width;
data.cell.contentHeight = Number.parseInt(dims[1]);
data.row.maxCellHeight = Object.keys(data.row.cells).reduce((a, key) => Math.max(a, data.row.cells[key].height), 0);
data.row.height = data.row.maxCellHeight;
return false;
}
}
}
}
};
},
},
Expand Down Expand Up @@ -228,14 +255,17 @@ export class Tabulator extends Table {

this._rows.push(tableRow);
};
private accessorDownload = (cellData: any, rowData: any, reason: string, _: any, columnComponent: any, rowComponent: any) => {
private accessorDownload = (question: Question) => (cellData: any, rowData: any, reason: string, _: any, columnComponent: any, rowComponent: any) => {
const columnDefinition = columnComponent.getDefinition();
const questionName = columnDefinition.field;
const column = this.columns.filter(col => col.name === questionName)[0];
if (!!column && rowComponent) {
const dataRow = this.data[rowComponent.getPosition()];
const dataCell = dataRow[questionName];
if (column.dataType === ColumnDataType.Image) {
if(question instanceof QuestionSignaturePadModel) {
return this.currentDownloadType === "pdf" && !!dataCell ? `<img-${question.signatureWidth}-${question.signatureHeight}>${dataCell}` : questionName;
}
return questionName;
}
if (column.dataType === ColumnDataType.FileLink && Array.isArray(dataCell)) {
Expand Down Expand Up @@ -302,7 +332,7 @@ export class Tabulator extends Table {
headerSort: false,
download: this._options.downloadHiddenColumns ? true : undefined,
formatter,
accessorDownload: this.accessorDownload,
accessorDownload: this.accessorDownload(question),
titleFormatter: (cell: any, formatterParams: any, onRendered: any) => {
return this.getTitleFormatter(
cell,
Expand Down

0 comments on commit 7477ae2

Please sign in to comment.