@@ -4,7 +4,9 @@ var common = require('../../../api/utils/common.js'),
44 moment = require ( 'moment-timezone' ) ,
55 log = require ( '../../../api/utils/log' ) ( 'reports:api' ) ,
66 ejs = require ( "ejs" ) ,
7+ fs = require ( "fs" ) ,
78 plugins = require ( '../../pluginManager.js' ) ,
9+ pdf = require ( '../../../api/utils/pdf' ) ,
810 { validateCreate, validateRead, validateUpdate, validateDelete, getUserApps, } = require ( '../../../api/utils/rights.js' ) ;
911
1012const FEATURE_NAME = 'reports' ;
@@ -414,15 +416,81 @@ const FEATURE_NAME = 'reports';
414416 if ( err2 ) {
415417 common . returnMessage ( params , 200 , err2 ) ;
416418 }
419+ else if ( res ) {
420+ var html = res . message ;
421+ if ( result . report_type !== "core" ) {
422+ html = ejs . render ( res . message . template , res . message . data ) ;
423+ }
424+
425+ common . returnRaw ( params , 200 , html , { 'Content-Type' : 'text/html; charset=utf-8' , 'Access-Control-Allow-Origin' : '*' } ) ;
426+ }
417427 else {
418- if ( params && params . res ) {
419- var html = res . message ;
420- if ( result . report_type !== "core" ) {
421- html = ejs . render ( res . message . template , res . message . data ) ;
422- }
428+ common . returnMessage ( params , 200 , 'No data to report' ) ;
429+ }
430+ } ) ;
431+ } ) ;
432+ } ) ;
433+ break ;
434+ case 'pdf' :
435+ validateRead ( paramsInstance , FEATURE_NAME , function ( ) {
436+ var params = paramsInstance ;
437+ var argProps = {
438+ '_id' : { 'required' : true , 'type' : 'String' }
439+ } ,
440+ id = '' ;
441+
442+ if ( ! ( id = common . validateArgs ( params . qstring . args , argProps ) . _id ) ) {
443+ common . returnMessage ( params , 200 , 'Not enough args' ) ;
444+ return false ;
445+ }
446+ common . db . collection ( 'reports' ) . findOne ( recordUpdateOrDeleteQuery ( params , id ) , function ( err , result ) {
447+ if ( err || ! result ) {
448+ common . returnMessage ( params , 200 , 'Report not found' ) ;
449+ return false ;
450+ }
423451
424- common . returnRaw ( params , 200 , html , { 'Content-Type' : 'text/html; charset=utf-8' , 'Access-Control-Allow-Origin' : '*' } ) ;
452+ // TODO: Handle report type check
453+
454+ reports . getReport ( common . db , result , function ( err2 , res ) {
455+ if ( err2 ) {
456+ common . returnMessage ( params , 200 , err2 ) ;
457+ }
458+ else if ( res ) {
459+ var html = res . message ;
460+ if ( result . report_type !== "core" ) {
461+ html = ejs . render ( res . message . template , res . message . data ) ;
425462 }
463+ const filePath = '/tmp/email_report_' + new Date ( ) . getTime ( ) + '.pdf' ;
464+ const options = { "path" : filePath , "width" : "1028px" , height : "1000px" } ;
465+
466+ pdf . renderPDF ( html , function ( ) {
467+ //output created file to browser
468+ fs . readFile ( filePath , function ( err3 , data ) {
469+ if ( err3 ) {
470+ console . log ( err3 ) ;
471+ common . returnMessage ( params , 500 , 'Cannot read pdf file' ) ;
472+ }
473+ else {
474+ common . returnRaw ( params , 200 , data , {
475+ 'Content-Type' : 'application/pdf' ,
476+ 'Content-Disposition' : 'inline; filename="report.pdf"' ,
477+ 'Content-Length' : data . length ,
478+ 'Access-Control-Allow-Origin' : '*'
479+ } ) ;
480+ }
481+ fs . unlink ( filePath , function ( unlinkErr ) {
482+ if ( unlinkErr ) {
483+ console . log ( "Cannot remove temp pdf file" ) ;
484+ console . log ( unlinkErr ) ;
485+ }
486+ } ) ;
487+ } ) ;
488+ } , options , {
489+ args : [ '--no-sandbox' , '--disable-setuid-sandbox' , '--disable-web-security' ] ,
490+ } , true ) ;
491+ }
492+ else {
493+ common . returnMessage ( params , 200 , 'No data to report' ) ;
426494 }
427495 } ) ;
428496 } ) ;
0 commit comments