0

I want to send some html content via jQuery to tcpdf and create an pdf. I have this in my index.php:

$('#test').click(function() {
   var result='hello';
    $.ajax({
     type: 'POST',
     url: 'pdf.php',
     data: {result: result},         
    });
})

and the pdf.php:

<?php
$result = '';
 if(isset($_POST['result'])) {
    $result = $_POST['result'];
 }

require_once('tcpdf/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetTitle('Production sheet');
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH);
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetFont('helvetica', '', 9);

$pdf->AddPage();

$html = '
      <h1>TEST</h1> 
      <table>
      <tr><td>'.$result.'</td></tr>           
      </table>       
      ';
$pdf->writeHTML($html, true, 0, true, 0);
$pdf->lastPage();
$pdf->Output('test.pdf', 'I');
?>

If I define variable result ($result = "Hello";) and i run directly pdf.php it's working perfect. It seems that for some reason my post isn't taken by pdf.php... Some ideas?

Thank you!

remux -
  • 37
  • 7
  • The code does look correct. Did you tried to echo out the $result to check if it gets passed on correctly before accessing tcpdf? I might be too tired but cannot see any mistake in the code. – Emil Borconi Apr 26 '16 at 20:29
  • `alert(result);` displays correctly. If I add `success:function(data){$('#testDiv').html(data);}` displays my variable 'hello'... Something it's weird here... – remux - Apr 26 '16 at 20:41
  • Thank you all for help. Emil's answer did the trick. – remux - Apr 27 '16 at 17:45

2 Answers2

1

I did say I'm too tired. As @andrew pointed out you were using inline output instead of download and in an Ajax call that won't really work. Reading through the comments if you want to Download the PDF and POST values to it here is my suggestions:

1) Create a FORM in your HTML

<form action='pdf.php' method=post target=_blank id=myform>
</form>

2) If you need to add data with Javascript then change your jQuery code as follow:

$('#test').click(function() {
   var result='hello';
   $("#my_form").append("<input type=hidden name=result value='"+result+'>");
   $("#my_form").submit();
});

3) Adjust the pdf.php to use $_POST instead of $_GET

You should consider if you really need an jQuery for this. If the result variable in your javascript get populated from user inputted form values you might just re-design your page and include all that in the form without the need of any additional jQuery at all.

Emil Borconi
  • 3,326
  • 2
  • 24
  • 40
0

You're using

$pdf->Output('test.pdf', 'I');

I meaning destination: Inline. See the manual

You either have to use option F and specify a file name on the server then redirect the user to that filename or use option D to force a download.

Something like:

$('#test').click(function() {
   var result='hello';
    window.open('/pdf.php?result='+result, '_blank');
});

And

<?php
$result = '';
 if(isset($_GET['result'])) {
    $result = $_GET['result'];
 }

 .....

 $pdf->Output('test.pdf', 'D');

EDIT

For large amounts of data you would be better to use the ajax method you were experimenting with, but handling the returned pdf document in the ajax response would be tricky, it is probably doable with a javascript file reader

As simpler option would look something like this:

$('#test').click(function() {
   var result='hello';
    $.ajax({
     type: 'POST',
     url: 'pdf.php',
     data: {result: result},         
    }).done(function(filename){
         $('#testDiv').html('<a target="_blank" href="/'+filename+'">download your pdf</a>');
   });
})

And

<?php
$result = '';
 if(isset($_POST['result'])) {
    $result = $_POST['result'];
 }

 ....

 $filename = time().'.pdf';
 $pdf->Output($filename, 'F');
 echo $filename;

so whats happening is you're creating pdf files on the server and appending a link to the page using ajax, its not the best solution as it leaves all pdf files on the server and the user only gets to see the link once.

Should hopefully give you some ideas though.

andrew
  • 9,313
  • 7
  • 30
  • 61
  • Thank You. It's working. But $_GET will work with large amount of data? – remux - Apr 26 '16 at 21:07
  • The hello thing was for test purposes. The real data it's way bigger and I think the only valid solution it is POST. I've tried with option D but it's not downloading neighter... – remux - Apr 26 '16 at 21:14
  • I edit the answer, should hopefully give you some idea of what can be done – andrew Apr 26 '16 at 21:36
  • I've tried your solution saving the file and I get a fopen error. It's for the file permissions.. but I don't want to save it anyway. But this prove that the POST is taken, and everything it's working perfectly, but for some reason tcpdf don't open (and don't download). Hmmm... – remux - Apr 26 '16 at 21:54
  • If you cant save files to the server then that option is out of the window. You should see this [question](http://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax) I think that receiving a file over ajax is not possible, but the author of the accepted answer created a jQuery plugin, I'm not sure how it works, seems to make some clever use of an ifame – andrew Apr 26 '16 at 23:13