Adorners

Example of using SurveyPDF internal code to render custom elements

                        function renderSurvey(survey, element) {
  ReactDOM.render(<Survey.Survey model={survey} />, element);
}



    
        Survey.StylesManager.applyTheme("orange");
    


var json = {
  "questions": [
    {
      "type": "text",
      "title": "Bottom description",
      "name": "pdf_adorners_bottomdesc"
    },
    {
      "type": "checkbox",
      "title": "Render checkbox question as radiogroup",
      "name": "pdf_adorners_checkboxasradio",
      "choices": [ "A", "B" ]
    },
    {
      "type": "comment",
      "title": "Render comment question as html",
      "name": "pdf_adorners_commentashtml",
      "defaultValue": "Sed venenatis nisl mi, eget lobortis augue venenatis ac.\n\nUt consectetur, nunc a tristique tempor, enim neque porttitor urna, non accumsan diam sem at erat. Suspendisse in sapien ac ligula aliquam porta a eu lorem"
    }
  ]
};

window.survey = new Survey.Model(json);


    survey.onComplete.add(function(result) {
        document.querySelector('#surveyResult').textContent =
            "Result JSON:\n" + JSON.stringify(result.data, null, 3);
    });


ReactDOM.render(<Survey.Survey model={survey}  />, document.getElementById("surveyElement"));

function saveSurveyToPdf(filename, surveyModel, pdfWidth, pdfHeight) {
   var options = {
        format: [pdfWidth, pdfHeight]
    };
    var surveyPDF = new SurveyPDF.SurveyPDF(json, options);
    surveyPDF.data = surveyModel.data;
    surveyPDF.onRenderQuestion.add(function (survey, options) {
        if (options.question.name !== "pdf_adorners_bottomdesc") return;
        var plainBricks = options.bricks[0].unfold();
        var lastBrick = plainBricks[plainBricks.length - 1];
        var point = SurveyPDF.SurveyHelper.createPoint(lastBrick);
        return new Promise(function (resolve) {
            SurveyPDF.SurveyHelper.createDescFlat(point, options.question,
                options.controller, 'Some description').then(function (descBrick) {
                options.bricks.push(descBrick);
                resolve();
            });
        });
    });
    surveyPDF.onRenderQuestion.add(function (survey, options) {
        if (options.question.name !== "pdf_adorners_checkboxasradio") return;
        var flatRadiogroup = options.repository.create(survey,
            options.question, options.controller, "radiogroup");
        return new Promise(function (resolve) {
            flatRadiogroup.generateFlats(options.point).then(function(radioBricks) {
                options.bricks = radioBricks;
                resolve();
            });
        });
     });
    surveyPDF
        .onRenderQuestion
        .add(function (survey, options) {
            if (options.question.getType() === "comment") {
                var htmlQuestion = Survey.QuestionFactory.Instance.createQuestion("html", "html_question");
                var paragraphs = options.question.value.split("\n");
                htmlQuestion.html = "";
                paragraphs.forEach(p => htmlQuestion.html += "<p>" + p + "</p><br>");
                var flatHtml = options
                    .repository
                    .create(survey, htmlQuestion, options.controller, "html");
                var commentBricks = options.bricks[0].unfold();
                var commentBrick = commentBricks.pop();
                var point = SurveyPDF.SurveyHelper.createPoint(commentBrick, true, true);
                return new Promise(function (resolve) {
                    flatHtml
                        .generateFlats(point)
                        .then(function (htmlBricks) {
                            options.bricks = commentBricks;
                            options.bricks.push(...htmlBricks);
                            resolve();
                        });
                });
            }
        });
    surveyPDF.save(filename);
}

document.getElementById("saveToPDFbtn").onclick = function () {
  var pdfWidth = survey.pdfWidth || 210;
  var pdfHeight = survey.pdfHeight || 297;
  saveSurveyToPdf("surveyResult.pdf", survey, pdfWidth, pdfHeight);
}

                    
<!DOCTYPE html>
<html>
<head>
    <title>Example of using SurveyPDF internal code to render custom elements, Reactjs Survey Library Example</title>


    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://unpkg.com/jquery"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.6.0/polyfill.js"></script>
    <script src="https://unpkg.com/react@15/dist/react.js"></script>
    <script src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
    <script src="https://unpkg.com/@babel/standalone@7.2.5/babel.min.js"></script>
<script src="https://surveyjs.azureedge.net/1.1.24/survey.react.js"></script>
<link href="https://surveyjs.azureedge.net/1.1.24/survey.css" type="text/css" rel="stylesheet" />
    <link rel="stylesheet" href="./index.css">
<script src="https://unpkg.com/jspdf@latest/dist/jspdf.min.js"></script>
<script src="https://surveyjs.azureedge.net/1.1.24/survey.pdf.js"></script>
</head>
<body>
    <button id="saveToPDFbtn" style="margin:10px">Save to PDF</button>            <div id="surveyElement">
            </div>
    <div id="surveyResult"></div>

<script type="text/babel" src="./index.js"></script>

</body>
</html>

SurveyPDF adorners

You may use onRenderQuestion event to render custom elements in PDF document using internal SurveyPDF helper methods and objects

The event is fired for every rendered question. Event accepts two parameters: instance of SurveyPDF and instance of AdornersOptions object


let surveyPDF = new SurveyPDF.SurveyPDF(json);
surveyPDF.onRenderQuestion.add(function (survey, options) {
    if (options.question.name === "question_to_remove_from_pdf") {
        options.bricks = [];
    });

Bricks

AdornersOptions object contains bricks property with array of SurveyPDF internal objects which represents PDF document elements (texts, boxes, images, etc.). You may modify this array to perform custom rendering. If you leave this array without any changes then PDF will be rendered as by default

Question

AdornersOptions's question property is SurveyJS question which is rendered now

Controller

AdornersOptions has controller property which is instance of DocController object. It is required parameter to many SurveyPDF internal methods

let surveyPDF = new SurveyPDF.SurveyPDF(json);
surveyPDF.onRenderQuestion.add(function (survey, options) {
    if (options.question.name !== "filter questions like this") return;
    //SurveyPDF bricks may be composite and contain many inside one
    //call unfold() method to get plain array with all bricks inside one
    var plainBricks = options.bricks[0].unfold();
    var lastBrick = plainBricks[plainBricks.length - 1];
    //SurveyPDF has SurveyHelper object with set of useful methods
    //e.g. createPoint(rect: IRect, isLeft: boolean = true, isTop: boolean = false): IPoint
    var point = SurveyPDF.SurveyHelper.createPoint(lastBrick);
    return new Promise(function (resolve) {
        SurveyPDF.SurveyHelper.createDescFlat(point, options.question,
            options.controller, 'Some description').then(function (descBrick) {
            options.bricks.push(descBrick);
            resolve();
        });
    });
});

Point

AdornersOptions has point property (with xLeft, yTop properties) which represent point in PDF page after which you can render custom bricks below it. It is not recommended to draw above this point because you can overlap new bricks with previous

Repository

AdornersOptions also has repository property which is instance of FlatRepository object. FlatRepository has create method which instantiate FlatQuestion and it subclasses, which can be used to generate bricks with default SurveyPDF render. You may modify this bricks to achieve desired custom view

let surveyPDF = new SurveyPDF.SurveyPDF(json);
surveyPDF.onRenderQuestion.add(function (survey, options) {
    if (options.question.name !== "some_checkbox_question") return;
    //create radiogroup flatQuestion to render checkbox as radigroup
    var flatRadiogroup = options.repository.create(survey,
        options.question, options.controller, "radiogroup");
    return new Promise(function (resolve) {
        flatRadiogroup.generateFlats(options.point).then(function(radioBricks) {
            options.bricks = radioBricks;
            resolve();
        });
    });
});