surveyjs / survey-pdf

Supplementary component to the SurveyJS Form Library to download surveys as PDF files and generate editable PDF forms.
https://surveyjs.io/pdf-generator/examples/survey-pdf-export/
Other
58 stars 29 forks source link

Export to PDF with markdown crashes on empty lines #294

Closed nbelyh closed 5 months ago

nbelyh commented 5 months ago

Are you requesting a feature, reporting a bug or asking a question?

reporting a bug

What is the current behavior?

PDF export crashes on empty lines (when using markdown)

What is the expected behavior?

PDF export does not crash, but produces empty lines when asked to

How would you reproduce the current behavior (if this is a bug)?

I am using surveyjs with markdown to render labels, and export to PDF.

When on the website, all works fine; however when exporting to PDF, empty line crashes the export:

Full example to reproduce on codepen: https://codepen.io/nbelyh/pen/poYdLpy

The crash happens if the "title" contains <br><br> sequence (empty line)

The crash is happening on this line: https://github.com/surveyjs/survey-pdf/blob/0aa70b911fd996bf78c2bed1be84b70d5e81662c/src/jspdf_plugins/from_html.js#L1082

I get this error: image

Full source code (modified "getting started" example, added the markdown function). Same as above on codepen.

const json = {
  "pages": [{
    "elements": [{
      "type": "comment",
      "name": "suggestions",
      "title": "What would make <br><br>you more **satisfied** with our product?"
    }]
  }],
  "showQuestionNumbers": false
};

  function processMarkdown(survey, options) {
    //convert the markdown text to html
    let str = options.text;
    str = str.replace(/^(\d+)\./, '$1');
    str = str.replace(/^(\d+)\)/, '$1');
    str = marked.marked(str);
    // remove root paragraphs <p></p>
    str = str.substring(3);
    str = str.substring(0, str.length - 5);
    // set html
    options.html = str;
  }

function createSurveyPdfModel(surveyModel) {

    let pdfWidth = !!surveyModel && surveyModel.pdfWidth ? surveyModel.pdfWidth : 210;
    let pdfHeight = !!surveyModel && surveyModel.pdfHeight ? surveyModel.pdfHeight : 297;
    let options = {
        fontSize: 14,
        margins: {
            left: 10,
            right: 10,
            top: 10,
            bot: 10
        },

        format: [pdfWidth, pdfHeight]
    };
    const surveyPDF = new SurveyPDF.SurveyPDF(json, options);
    surveyPDF.onTextMarkdown.add(processMarkdown);
    if (surveyModel) {
        surveyPDF.data = surveyModel.data;
    }

    return surveyPDF;
}
function saveSurveyToPdf(filename, surveyModel) {
    createSurveyPdfModel(surveyModel).save(filename);
}

function SurveyPdfComponent() {

    const survey = new Survey.Model(json);
    survey.addNavigationItem({
        id: "survey_save_as_file", title: "Save as PDF", action: () => { saveSurveyToPdf("surveyResult.pdf", survey); }
    });
    survey.onTextMarkdown.add(processMarkdown);
    survey.data = {};
    return (<SurveyReact.Survey model={survey} />);
}

const root = ReactDOM.createRoot(document.getElementById("surveyElement"));
root.render(<SurveyPdfComponent />);

Specify your

nbelyh commented 5 months ago

Ups. Have not seen that this is a duplicate. Closing as a duplicate of: https://github.com/surveyjs/survey-pdf/issues/287