0

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);
 ?>
Aby
  • 75
  • 10
  • So no errors/crashes here, just no PDF where you expect? What does $pdfoutput look like? Does it looks like PDF content or is it empty? Also, the path you are writing to looks relative, are you sure you can get there from your PHP working directory? – killthrush Aug 07 '15 at 09:37
  • did you call this code inside any js..??? – Abin Aug 07 '15 at 12:29
  • @killthrush I have a js file where I display the 2 images I mentioned inside 2 canvas. Then the images are posted to php via ajax for saving to server. I have shown the code above in the question where the images are recieved as data1 and data2 and is saved to server. This all happens with no errors but am unable to generate the pdf of these images. – Aby Aug 11 '15 at 13:04
  • @Aby can you confirm that you can write a simple text file using PHP to the same location where you are trying to write the PDF? If you can, then that rules out any permissions or path issues. – killthrush Aug 11 '15 at 21:52
  • Are you still having problems (no answer selected)? If so (and if you're even still using dompdf), do you get a PDF at all? One potential issue is that you are including dompdf_config.inc.php twice (I don't believe that would cause a failure, but I don't recall at the moment). Also, you're including the image path in the HTML, not referencing it as an image. – BrianS Oct 05 '15 at 17:50

3 Answers3

0

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.

Community
  • 1
  • 1
killthrush
  • 4,859
  • 3
  • 35
  • 38
0

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

$dompdf = new DOMPDF();
Wolfgang Blessen
  • 900
  • 10
  • 29
  • The docs are silent on this point, although this does not appear to be a requirement for this in PHP (unless the constructor takes an argument). There are a [few](http://stackoverflow.com/q/1945989/264628) [questions](http://stackoverflow.com/q/28914283/264628) on this [topic](http://stackoverflow.com/q/9325491/264628). – BrianS Oct 05 '15 at 18:00
-3

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

Amit
  • 1,836
  • 15
  • 24