Aby
Aby

Reputation: 75

Unable to generate pdf using DOMPDF

I have a php file which saves two images into server and creating a pdf of this saved images using dompdf. I am able to save the images into particular folder but not able to generate pdf. could someone tell me what am doing wrong here? Here is my code.

<?php

 ini_set('display_errors', 1);
 error_reporting(E_ALL ^ E_NOTICE);
 $data1 = $_POST['data1'];
 $data2 = $_POST['data2'];

 if(isset($data1)) {
 $uri1 =  substr($data1,strpos($data1,",")+1);
 $uri2 =  substr($data2,strpos($data2,",")+1);

 $path =$_SERVER["DOCUMENT_ROOT"].'/divya/custom_product/sites/default/files/cart';
 $id = "test";
 $type ="order";
 $file1 = $path .'/'.$id.'-'.$type.'1.png';
 $file2 = $path .'/'.$id.'-'.$type.'2.png';

 $a=base64_decode($uri1);
 $b=base64_decode($uri2);

 file_put_contents($file1, $a);
 file_put_contents($file2, $b);
 }
 ?>
 <?php
 require_once("dompdf_config.inc.php");
 require_once("sites/all/modules/print/lib/dompdf/dompdf_config.inc.php");


 $tbl = '<html style="margin:20px 20px 0px; padding:0;">
 <body style="margin:0; padding:0;">

 <table style="margin:0px 0px 5px 3px;">
 <tr>
 <td style="width:220px;vertical-align:top;">'.$file1.'</td>
 <td style="width:220px;vertical-align:top;">'.$file2.'</td>
 </tr>
 </table>

 </body>
 </html>';
 $dompdf = new DOMPDF;
 $dompdf->load_html($tbl);
 $dompdf->render();
 $pdfoutput = $dompdf->output();
 //  Checks whether there is an output folder inside sites/default/files
 if (!is_dir('public://output')) {
 mkdir("public://output", 0777);
 //  Creates a folder and changes its permissions}
 $filename = 'sites/default/files/output/' . 'sample.pdf'
 $fp = fopen($filename, "w+");
 fwrite($fp, $pdfoutput);
 //  Writes the pdf output to a file
 fclose($fp);
 ?>

Upvotes: 0

Views: 1805

Answers (3)

Wolfgang Blessen
Wolfgang Blessen

Reputation: 1029

You are missing parantheses in the creation of the DOMPDF Class:

$dompdf = new DOMPDF();

Upvotes: 0

killthrush
killthrush

Reputation: 5107

After going through the DOMPDF wiki here, I realized that the default mode of operation is to stream the file back to the client, not necessarily save it to a file. So I started digging around some more.

This question was posted before; they use similar code to you, but use the much simpler file_put_contents() to write the file. It's still worth trying to write a simple text file first to that location just to rule out any file system issues.

Upvotes: 0

Amit
Amit

Reputation: 1885

Have you enabled error logging? Error logs, many a times, tell you exactly what is wrong

Upvotes: -3

Related Questions