0

I have created a pdf file of a page using DOMpdf as following.

<?php

use Dompdf\Dompdf;
class Pdfgenerator {

  public function generate($html, $filename='', $stream=TRUE, $paper = 'A4', $orientation = "portrait")
  {
    $dompdf = new DOMPDF(array('enable_remote' => true));
    $dompdf->loadHtml($html);
    $dompdf->setPaper($paper, $orientation);
    $dompdf->render();
    if ($stream) {
        $dompdf->stream($filename.".pdf", array("Attachment" => 0));
    } else {
        return $dompdf->output();
    }
  }
}

On clicking Save As Pdf button on a view page, pdf of the page is created in a controller function using DOMpdf and open the pdf in new tab in browser from where it can be downloaded on the download path which is set for browser.

$html = $this->load->view('test/pdfview', $this->data, true);
$this->load->library('pdfgenerator');
$filename = 'testfile';
$this->pdfgenerator->generate($html, $filename, true, 'a4', '');

What I actually want is to download the file on given path such that when a PDF is created a save prompt will open which already navigates to given location. If its the right location I can press save, if not I can navigate to the proper save location. These are somewhat similar questions already asked which I found but I couldnt find them helpful.

Cœur
  • 37,241
  • 25
  • 195
  • 267
whoosis
  • 454
  • 7
  • 25

2 Answers2

1

You can create a script like below in php which will send generated pdf to the browser

 <?php


use Dompdf\Dompdf;
class Pdfgenerator {

  public function downLoadPdf(){
        $name= "testfile.pdf";

        header('Content-Description: File Transfer');
        header('Content-Type: application/force-download');
        header("Content-Disposition: attachment; filename=\"" . basename($name) . "\";");
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($name));
        ob_clean();
        flush();
        readfile("your_file_path/".$name); //showing the path to the server where the file is to be download

    }
}
    ?>

Just add this function in your class and call it using ajax on click of save button (If it exists in your view page)
When you call this script from browser on clicking save button,Browser will automatically open save window and you can save the file at your desired location

Devang Naghera
  • 731
  • 6
  • 13
  • 1
    this doesnt seem to work . I added this code after creating pdf . i.e after `$this->pdfgenerator->generate($html, $filename, true, 'a4', '')` , the problem is same , the pdf opens in browser page and no save window appears. – whoosis Dec 31 '17 at 09:09
  • 1
    I have edited the answer.You should create a new function and call it on save button from your view. – Devang Naghera Dec 31 '17 at 09:20
  • 1
    I appreciate your help but I fail to understand how pdf will be generated with your snippet, i.e, on save button call, the function which convert html to pdf is called ( i.e. `$this->pdfgenerator->generate($html, $filename, true, 'a4', '')`) , if i call downloadPdf on save, `testfile.pdf` wont be even created yet to download ?? – whoosis Dec 31 '17 at 09:29
  • 1
    You have to save pdf somewhere when you call ``generate`` function. The snippet I have added in answer wil not generate pdf, It will send the saved pdf from your server location to browser. Your function is just generating the pdf file and displaying it in the browser.You have to save it somewhere in your server for downloading purpose.Hope you get my point – Devang Naghera Dec 31 '17 at 09:38
0
    // Codeigniter 4.2.6

    public function saveAsPdf($emp_code="00000")
    {
        $dompdf = new Dompdf();
        $data = [
            'imageSrc'    => "#",
            'name'         => 'John Doe',
            'address'      => 'USA',
            'mobileNumber' => '000000000',
            'email'        => 'john.doe@email.com'
        ];
        $html = view('resume', $data);
        $dompdf->loadHtml($html);
        $dompdf->render();
        // $dompdf->stream('resume.pdf');
        $output = $dompdf->output();
        $flname = $emp_code.".pdf";
        file_put_contents(WRITEPATH . 'uploads/employee/'.$flname, $output);

        $insdata     = array(
            'emp_code' => $in_title??202222,                             
            'file_name' => $flname,                             
            'status' => 1,                        
            'created_at' => date('Y-m-d H:i:s')                
        );

        $database = \Config\Database::connect();
        $db = $database->table('emp_payslip');
        $save = $db->insert($insdata);
        
    }
Sonu Chohan
  • 141
  • 1
  • 5