Survey - Fix pdf export BT#17835

pull/3659/head
Julio Montoya 5 years ago
parent 0f6468b126
commit a7578e88b1
  1. 67
      main/survey/reporting.php
  2. 34
      main/survey/surveyUtil.class.php

@ -141,12 +141,19 @@ $htmlHeadXtra[] = api_get_js('d3/d3.v3.5.4.min.js');
$htmlHeadXtra[] = api_get_js('dimple.v2.1.2.min.js');
$htmlHeadXtra[] = '<script>
async function exportToPdf() {
window.jsPDF = window.jspdf.jsPDF;
$(".question-item img").hide();
$(".question-item video").hide();
$(".question-item audio").hide();
var doc = document.getElementById("question_results");
var pdf = new jsPDF("", "pt", "a4");
//var a4Height = 841.89;
// Adding title
pdf.setFontSize(16);
pdf.text(40, 40, "'.get_lang('Reporting').'");
@ -163,7 +170,6 @@ async function exportToPdf() {
var pages = [];
var page = 1;
for (var i = 0; i < divs.length; i += 1) {
const svg = divs[i].querySelector("svg");
// Two parameters after addImage control the size of the added image,
// where the page height is compressed according to the width-height ratio column of a4 paper.
if (!pages[page]) {
@ -171,11 +177,6 @@ async function exportToPdf() {
}
var positionY = 180;
/*var positionY = headerY + 180;
if (positionY > 800) {
pdf.addPage();
page++;
}*/
pages[page] += 1;
var diff = 250;
if (page > 1) {
@ -191,27 +192,19 @@ async function exportToPdf() {
pdf.setFontSize(12);
pdf.text(40, positionY, title.text());
var svg = divs[i].querySelector("svg");
svg2pdf(svg, pdf, {
xOffset: 10,
yOffset: positionY+10,
scale: 0.5
yOffset: positionY + 10,
scale: 0.45
});
const tables = divs[i].getElementsByClassName("table");
var config= {
ignoreElements: function (element) {
if (element.nodeName == "IMG" || element.nodeName =="VIDEO"|| element.nodeName =="AUDIO") {
return true;
}
return false;
}
};
var tables = divs[i].getElementsByClassName("display-survey");
var config= {};
for (var j = 0; j < tables.length; j += 1) {
await html2canvas(tables[j], config).then(function(canvas) {
var pageData = canvas.toDataURL("image/jpeg", 0.8);
pdf.addImage(pageData, "JPEG", 40, positionY + 210, 520, 530.28/canvas.width * canvas.height);
pdf.addImage(pageData, "JPEG", 40, positionY + 200, 500, 500/canvas.width * canvas.height);
});
}
@ -219,38 +212,12 @@ async function exportToPdf() {
pdf.addPage();
page++;
}
/*await html2canvas(divs[i], {}).then(function(canvas) {
// Returns the image data URL, parameter: image format and clarity (0-1)
var pageData = canvas.toDataURL("image/jpeg", 0.8);
// Two parameters after addImage control the size of the added image,
// where the page height is compressed according to the width-height ratio column of a4 paper.
if (!pages[page]) {
pages[page] = 0;
}
pages[page] += 1;
var positionY = 200;
var diff = 250;
if (page > 1) {
positionY = 60;
diff = 220;
}
if (pages[page] > 1) {
positionY = pages[page] * diff + 10;
}
pdf.addImage(url, "JPEG", 40, positionY, 530, 530.28/canvas.width * canvas.height);
//pdf.addImage(pageData, "JPEG", 40, positionY, 530, 530.28/canvas.width * canvas.height);
if (i > 0 && (i -1) % 2 === 0) {
pdf.addPage();
page++;
}
});*/
}
$(".question-item img").show();
$(".question-item video").show();
$(".question-item audio").show();
pdf.save("reporting.pdf");
}
</script>';

@ -667,6 +667,7 @@ class SurveyUtil
$chartData = [];
$options = [];
$questionId = (int) $question['question_id'];
echo '<div class="title-question">';
echo strip_tags(isset($question['survey_question']) ? $question['survey_question'] : null);
echo '</div>';
@ -737,13 +738,13 @@ class SurveyUtil
echo '</div>';
// displaying the table: headers
echo '<table class="display-survey table" id=table_'.$chartContainerId.'>';
echo '<table class="display-survey table" id="table_'.$chartContainerId.'">';
echo ' <tr>';
echo ' <th>&nbsp;</th>';
echo ' <th>'.get_lang('AbsoluteTotal').'</th>';
echo ' <th>'.get_lang('Percentage').'</th>';
echo ' <th>'.get_lang('VisualRepresentation').'</th>';
echo ' <tr>';
echo ' </tr>';
// Displaying the table: the content
if (is_array($options)) {
@ -778,15 +779,18 @@ class SurveyUtil
echo '0';
}
echo ' </td>';
echo ' <td class="center">'.round($answers_number, 2).' %</td>';
echo ' <td class="center">';
echo '</td>';
echo '<td class="center">'.round($answers_number, 2).' %</td>';
echo '<td class="center">';
$size = $answers_number * 2;
if ($size > 0) {
echo '<div style="border:1px solid #264269; background-color:#aecaf4; height:10px; width:'
.$size.'px">&nbsp;</div>';
echo '<div
style="border:1px solid #264269; background-color:#aecaf4; height:10px;
width:'.$size.'px">
&nbsp;
</div>';
} else {
echo '<div style="text-align: left;">'.get_lang("NoDataAvailable").'</div>';
echo '<div style="text-align: left;">'.get_lang('NoDataAvailable').'</div>';
}
echo ' </td>';
echo ' </tr>';
@ -803,13 +807,13 @@ class SurveyUtil
}
// displaying the table: footer (totals)
echo ' <tr>';
echo ' <td class="total"><b>'.get_lang('Total').'</b></td>';
echo ' <td class="total"><b>'.$optionResult.'</b></td>';
echo ' <td class="total">&nbsp;</td>';
echo ' <td class="total">&nbsp;</td>';
echo ' </tr>';
echo '</table>';
echo ' <tr>
<td class="total"><b>'.get_lang('Total').'</b></td>
<td class="total"><b>'.$optionResult.'</b></td>
<td class="total">&nbsp;</td>
<td class="total">&nbsp;</td>
</tr>
</table>';
}
echo '</div>';
}

Loading…
Cancel
Save