KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Need PDF output returned as Base64 #212

Open High5 software opened this topic on on Feb 2, 2018 - 3 comments

High5 software commented on Feb 2, 2018

Hi, I am integrating KoolReport Pro into our system, and I was wondering if there was a simple way to set the pdf output as a variable or base64 encode it to be returned as a json object. Fore example, the sakila_rental example uses the following code -

$report->run()
->export('SakilaRentalPdf')
->pdf(array(
    "format"=>"A4", 
    "orientation"=>"portrait"))
->toBrowser("sakila_rental.pdf");

What I'd like to do is -

$resultOutput = $report
->run()
->export('SakilaRentalPdf')
->pdf(array( 
    "format"=>"A4", 
    "orientation"=>"portrait"
))->toBrowser("sakila_rental.pdf");
$encodedResult = base64_encode($resultOutput);

Any help with this is much appreciated.

Update: have this sorted, added a custom function to "File" package.

KoolReport commented on Feb 3, 2018

Awesome solution

You are advanced user now. In next version we will make this feature available for Export as well. Thank for your great suggestion.

High5 software commented on Feb 5, 2018

To those who need the same functionality, I added the following to koolreports/packages/export/File.php. It would require you have the PDF export package.

public function toBase64() {
    $source = realpath($this->path);
    $file = @fopen($source,"rb");
    if ($file) {
        $result = file_get_contents($source);
        @fclose($file);
        return base64_encode($result);
    }
}

It can be assigned to a variable as follows

$base64PDF = $report
->export('SakilaRentalPdf')
->pdf(array(
    "format"=>"A4",
    "orientation"=>"portrait"
))
->toBase64();

//echo out the base64 string or json here also
echo $base64PDF;
KoolReport commented on Feb 5, 2018

Thanks for sharing your code.

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
None yet

None