1

I have installed tcpdf in the server. I have checked file for existence. In a javascript function I am calling print.php through ajax.

onclick = "savepdf();"

The javascript fuction (after initiating ajax) is:

function savepdf(){
    var html = 'This is a test page.';
    var parameters = "content="+html;
    ajaxRequest.open("POST", 'print.php', true);
    ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            alert('Success');
        }
    };
    ajaxRequest.send(parameters);
}

This is the print.php, which I have copied from its stock example.

<?php
    header("Content-Type: application/octet-stream");
    require_once('/tcpdf/tcpdf.php'); 
    $text = $_POST['content'];
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('SOMEONE');
    $pdf->SetTitle('Report');
    $pdf->SetSubject('Report');
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    $pdf->setFontSubsetting(true);
    $pdf->SetFont('dejavusans', '', 10, '', true);
    $pdf->AddPage();
    $html = $text;
    $pdf->writeHTML($html, true, false, true, false, '');
    $pdf->Output($_SERVER['DOCUMENT_ROOT'] . '/temp/output.pdf', 'F');
    $file = $_SERVER['DOCUMENT_ROOT'] . '/temp/output.pdf';
    header('Content-Disposition: attachment; filename="'.$file.'"');
    header('Content-Length: ' . filesize($file));
    header("Cache-control: private"); //use this to open files directly                     
    readfile($file);
?>

The file is physically saved on the server, but doesn't download. How do I troubleshoot?

Marek Skiba
  • 2,124
  • 1
  • 28
  • 31
sridhar
  • 1,321
  • 5
  • 17
  • 26
  • 1
    Does your print.pdf work when you call it without ajax? Where do you create your `$pdf` object? – Gerald Schneider Jan 14 '16 at 07:24
  • I missed pasting that line initially, edited it. Yes, PDF is created when called directly, but has no submitted text. – sridhar Jan 14 '16 at 07:34
  • Can you try `setRequestHeader('Content-Type', 'application/pdf')` or `setRequestHeader('Content-Type', 'application/force-download')` ? – choz Jan 14 '16 at 07:36

1 Answers1

0

You cannot force user download a file via AJAX request. You need to redirect the browser to the generated file(if not private), or to a php script that will output it. See the discussion here: https://stackoverflow.com/a/9970672/1800369

Community
  • 1
  • 1
Plamen Nikolov
  • 2,643
  • 1
  • 13
  • 24
  • It was a flawed concept. I gave it up. Instead, I made a form, which submitted the contents and generated PDF successfully. – sridhar Jan 15 '16 at 13:21