Skip to content

Commit

Permalink
Dev: more work done on the implementation of the new jspdf library
Browse files Browse the repository at this point in the history
  • Loading branch information
lacrioque committed Nov 24, 2016
1 parent 5ca3f0f commit 948510a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
4 changes: 4 additions & 0 deletions application/config/third_party.php
Expand Up @@ -318,6 +318,10 @@
'js' => array(
'jspdf.debug.js'
// 'jspdf.min.js'
),
'depends' => array(
'jquery',
'bootstrap'
)
)

Expand Down
3 changes: 2 additions & 1 deletion application/views/admin/export/statistics_view.php
Expand Up @@ -9,7 +9,8 @@
<?php $this->renderPartial('/admin/export/statistics_subviews/_statistics_view_scripts', array('sStatisticsLanguage'=>$sStatisticsLanguage, 'surveyid'=>$surveyid, 'showtextinline'=>$showtextinline)) ; ?>
<div class="container-center">
<div class="row">
<div class="col-sm-4 col-sm-offset-4 col-xs-12"><button id="action_js_export_to_pdf" class="btn btn-primary btn-block"><?php eT("Export to PDF");?></button></div>
<div class="col-sm-3 col-sm-offset-3 col-xs-6"><button id="action_js_export_to_pdf" class="btn btn-primary btn-block"><?php eT("Export to PDF");?></button></div>
<div class="col-sm-3 col-xs-6"><button id="action_js_preview_to_print" class="btn btn-primary btn-block"><?php eT("Printable View");?></button></div>
</div>
</div>
<div id='statisticsview' class='side-body <?php echo getSideBodyClass(false); ?>'>
Expand Down
40 changes: 33 additions & 7 deletions scripts/admin/statistics.js
Expand Up @@ -180,9 +180,7 @@ function init_chart_js_graph_with_datas($type,$qid)
);
}

$(document).ready(function() {
//for nicely printed statistics
$('body').addClass('onStatistics');
var onDocumentReady = function(){

if ($('#completionstateSimpleStat').length>0)
{
Expand Down Expand Up @@ -267,8 +265,6 @@ $(document).ready(function() {
$type = $(this).data('type');
$qid = $(this).data('qid');

console.log($type);

// chartjs
if($type == 'Bar' || $type == 'Radar' || $type == 'Line' )
{
Expand Down Expand Up @@ -565,7 +561,10 @@ $(document).ready(function() {
{
changeGraphType('showpie', this.parentNode);
});
});
};
$(document).ready(onDocumentReady);
$(document).on('triggerReady', onDocumentReady);


var isWaiting = {};

Expand Down Expand Up @@ -730,13 +729,40 @@ function changeGraphType (cmd, id) {

}

/**
* Handlers and builders for the different question types based on their answers
* The html has to be recreated for the jspdf to work
*/
var elementHandlers = {
'thead': function(element, renderer){

}
}

$(document).ready(function(){

$('body').addClass('onStatistics');


$('body').on('click','#action_js_preview_to_print', function(){
var openWindow = window.open("about:blank","", "_blank");
var head = "";
$('head').find('link').each(function(i,item){head = head+item.outerHTML});
$('document').find('script').each(function(i,item){head = head+item.outerHTML});
var body = "";
$('#statisticsview').find('.statisticstable').each(function(i,item){body = body + item.outerHTML});

var html = "<html><head>" + head + "</head><body>" + body + "<body></html>" ;
openWindow.document.write(html);
$(openWindow.document).trigger('triggerReady');
console.log($('head'));
});
$('body').on('click','#action_js_export_to_pdf', function(){
var doc = new jsPDF();
console.log("Getting the pdf");
$('#statisticsview').find('.statisticstable').each(function(i, table){
doc.addPage();
doc.fromHTML(table, 210, 297, {width: 590});
doc.fromHTML(table, 210, 297, {width: 590, elementHandlers: elementHandlers});

});
var dataurl = doc.output('dataurlnewwindow');
Expand Down
23 changes: 17 additions & 6 deletions third_party/jspdf/jspdf.debug.js
Expand Up @@ -7013,20 +7013,28 @@ Q\n";
}
return isHandledElsewhere;
};
// This method is not working for more complex tables.

tableToJson = function tableToJson(table, renderer) {
var data, headers, i, j, rowData, tableRow, table_obj, table_with, cell, l;
data = [];
headers = [];
var $table, data, head, body, footer, i, j, rowData, tableRow, table_obj, table_width, cell, l;
$table = jQuery(table);
data = [];
head = {};
head = {};
i = 0;
l = table.rows[0].cells.length;
table_with = table.clientWidth;

//ok so the first row should define the length? ever heard of a headrow?
l = $table.find('thead').find('tr').find('td').length;


table_width = table.clientWidth;
while (i < table.rows.length) {
cell = table.rows[0].cells[i];
if(cell){
headers[i] = {
name: cell.textContent.toLowerCase().replace(/\s+/g, ''),
prompt: cell.textContent.replace(/\r?\n/g, ''),
width: cell.clientWidth / table_with * renderer.pdf.internal.pageSize.width
width: cell.clientWidth / table_width * renderer.pdf.internal.pageSize.width
};
}
i++;
Expand All @@ -7050,6 +7058,7 @@ Q\n";
headers: headers
};
};

var SkipNode = {
SCRIPT: 1,
STYLE: 1,
Expand All @@ -7058,7 +7067,9 @@ Q\n";
EMBED: 1,
SELECT: 1
};

var listCount = 1;

_DrillForContent = function DrillForContent(element, renderer, elementHandlers) {
var cn, cns, fragmentCSS, i, isBlock, l, px2pt, table2json, cb;
cns = element.childNodes;
Expand Down

0 comments on commit 948510a

Please sign in to comment.