@@ -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' ;
@@ -428,6 +430,69 @@ const FEATURE_NAME = 'reports';
428430 } ) ;
429431 } ) ;
430432 break ;
433+ case 'pdf' :
434+ validateRead ( paramsInstance , FEATURE_NAME , function ( ) {
435+ var params = paramsInstance ;
436+ var argProps = {
437+ '_id' : { 'required' : true , 'type' : 'String' }
438+ } ,
439+ id = '' ;
440+
441+ if ( ! ( id = common . validateArgs ( params . qstring . args , argProps ) . _id ) ) {
442+ common . returnMessage ( params , 200 , 'Not enough args' ) ;
443+ return false ;
444+ }
445+ common . db . collection ( 'reports' ) . findOne ( recordUpdateOrDeleteQuery ( params , id ) , function ( err , result ) {
446+ if ( err || ! result ) {
447+ common . returnMessage ( params , 200 , 'Report not found' ) ;
448+ return false ;
449+ }
450+
451+ // TODO: Handle report type check
452+
453+ reports . getReport ( common . db , result , function ( err2 , res ) {
454+ if ( err2 ) {
455+ common . returnMessage ( params , 200 , err2 ) ;
456+ }
457+ else {
458+ if ( params && params . res ) {
459+ var html = res . message ;
460+ if ( result . report_type !== "core" ) {
461+ html = ejs . render ( res . message . template , res . message . data ) ;
462+ }
463+ const filePath = '/tmp/email_report_' + new Date ( ) . getTime ( ) + '.pdf' ;
464+ const options = { "path" : filePath , "width" : "1028px" , height : "1000px" } ;
465+ pdf . renderPDF ( html , function ( ) {
466+ //output created file to browser
467+ fs . readFile ( filePath , function ( err3 , data ) {
468+ if ( err3 ) {
469+ console . log ( err3 ) ;
470+ common . returnMessage ( params , 500 , 'Cannot read pdf file' ) ;
471+ }
472+ else {
473+ common . returnRaw ( params , 200 , data , {
474+ 'Content-Type' : 'application/pdf' ,
475+ 'Content-Disposition' : 'inline; filename="report.pdf"' ,
476+ 'Content-Length' : data . length ,
477+ 'Access-Control-Allow-Origin' : '*'
478+ } ) ;
479+ }
480+ fs . unlink ( filePath , function ( unlinkErr ) {
481+ if ( unlinkErr ) {
482+ console . log ( "Cannot remove temp pdf file" ) ;
483+ console . log ( unlinkErr ) ;
484+ }
485+ } ) ;
486+ } ) ;
487+ } , options , {
488+ args : [ '--no-sandbox' , '--disable-setuid-sandbox' , '--disable-web-security' ] ,
489+ } , true ) ;
490+ }
491+ }
492+ } ) ;
493+ } ) ;
494+ } ) ;
495+ break ;
431496 case 'status' :
432497 validateUpdate ( paramsInstance , FEATURE_NAME , function ( ) {
433498 var params = paramsInstance ;
0 commit comments