Search code examples
phpangularjsmime-typeswkhtmltopdfphalcon

wkhtmltopdf php wrapper's send() not working


I am trying to convert my html page to pdf using wkhtmltopdf php wrapper with this library. I have tried to use saveAs() function, that works perfectly fine. But I want to download the generated pdf file on the browser so I used send(filename) function, unfortunately that's not working for me.

Here is my code :

public static function generatePDF(){
       $this->setJsonResponse();
       $data = $this->request->getPost();
       $srno = $data["number"];
       $type = $data["type"];
       $html = $data["html"];
       $pdf = new Pdf(array(
              'print-media-type',
              'commandOptions' => array(
                  'useExec' => true, 
                  'procEnv' => array(
                      'LANG' => 'en_US.utf-8',
                  )
       )));
       $pdf->binary = $config->application->wkhtmltopdf_binary;
       $pageOptions = array(
              'disable-smart-shrinking',
       );
       $pdf->addPage($html, $pageOptions);
       $filename = $type . "-" . $srno . '-' . date("Ymd") . '' . date("his") . ".pdf";
       if (!$pdf->send($filename)) {
           $error = $pdf->getError();
           return array('error' => $error);
       }
       return true;
  }

I debug the code and I found that there is some problem with MIME type. send() requires the MIME Type 'application/pdf' but in my response that is 'application/json'. And that's because of the first line of function $this->setJsonResponse();

Here is this function which is in my ControllerBase.php

protected function setJsonResponse() {
    $this->json_response = true;

    $this->view->disable();

    $this->response->setContentType('application/json', 'UTF-8');

    // If request AJAX is json type then parse it and update
    // php's POST member.
    $contentType = $this->request->getHeader('CONTENT_TYPE');
    if (strpos(strtolower($contentType), "application/json") !== FALSE) {
        $jsonRawBody = $this->request->getJsonRawBody(true);
        if ($this->request->getRawBody() && !$jsonRawBody) {
            // throw new Exception("Invalid JSON syntax");
        } else {
            $_POST = $jsonRawBody;
        }
    }
}

If I remove that function call from generatePDF, then I got $data empty. What should I do to make send() working and get $data not null from the ajax?

P.S.: I am using AngularJs on client-side and using http request.

Edit : http request (getJson is a global function I made for project. i.e. just a simple http reqeust or ajax)

$rootScope.getJson(controller + '/generatePdf', data, function (r) {
if (r.error) {
    console.log(r.error);
} else if (r.success) {
    var filename = r.success.filename;
    var url = r.success.base + 'temp/' + filename;
    var link = document.createElement('a');
    link.href = url;
    link.setAttribute('target', '_self');
    link.download = filename;
    link.click();
} else {
    console.log('something went wrong');
}

});


Solution

  • I have added this function in ControllerBase and calling this in generatePDF() :

    protected function setPdfResponse() {
        $this->json_response = false;
        $this->pdf_response = true;
    
        $this->view->disable();
    
        // Important response-headers copied from wkhtmltopdf
        $this->response->setContentType('application/pdf');
        $this->response->setHeader('Pragma', 'public');
        $this->response->setHeader('Expires', '0');
        $this->response->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
        $this->response->setHeader('Content-Transfer-Encoding', 'binary');
    
        // If request AJAX is json type then parse it and update
        // php's POST member.
        $contentType = $this->request->getHeader('CONTENT_TYPE');
        if (strpos(strtolower($contentType), "application/json") !== FALSE) {
            $jsonRawBody = $this->request->getJsonRawBody(true);
            if ($this->request->getRawBody() && !$jsonRawBody) {
                //throw new Exception("Invalid JSON syntax");
            } else {
                $_POST = $jsonRawBody;
            }
        }
    }
    

    This worked for me and here no need of java-script to build a pdf file.