Welcome Guest, Not a member yet? Register   Sign In
TCPDF send to browser
#1

Hi , i have a problem with TCPDF
Iwant send my code to browser ( from controller's method) with :
$pdf->Output($link_pdf, 'I');
but the result is strange characters :
https://postimg.cc/0zzfS4dt
I think i must set output to browser as pdf , how i can do ?
Reply
#2

Does $link_pdf have a pdf extension?
Reply
#3

i'm not a profi but this works for me so it might help
PHP Code:
class TPdfCustom extends Tcpdf
{
   -- 
lots of other stuff begin
   
-- lots of other stuff end  

   
private function pdfFilename(string $filename):string{
        
$file WRITEPATH.'cache/'.$filename;
        
$ext=pathinfo($filenamePATHINFO_EXTENSION);
        if(
$ext==''$file.='.pdf';
        return 
$file;
    }

    
/**
     * @param $response
     * @param string $filename
     * @return void
     */
    
public function sendToBrowser($responsestring $filename):void{
        
$file $this->pdfFilename($filename);
        
// save to file and then read the file
        
$this->Output($file'F');
        
$response->setHeader('Content-Type''application/pdf');
        
$response->setHeader('Content-Disposition''inline; filename="' $filename '"');
        
$response->setHeader('Content-Transfer-Encoding''binary');
        
$response->setHeader('Content-Length: 'filesize($file),'');
        
$response->setHeader('Accept-Ranges:''bytes');
        
readfile($file);
        
unlink($file);
    }

Reply
#4

(02-29-2024, 10:06 AM)badger Wrote: i'm not a profi but this works for me so it might help
PHP Code:
class TPdfCustom extends Tcpdf
{
  -- lots of other stuff begin
  
-- lots of other stuff end  

  
private function pdfFilename(string $filename):string{
        $file WRITEPATH.'cache/'.$filename;
        $ext=pathinfo($filenamePATHINFO_EXTENSION);
        if($ext==''$file.='.pdf';
        return $file;
    }

    /**
    * @param $response
    * @param string $filename
    * @return void
    */
    public function sendToBrowser($responsestring $filename):void{
        $file $this->pdfFilename($filename);
        // save to file and then read the file
        $this->Output($file'F');
        $response->setHeader('Content-Type''application/pdf');
        $response->setHeader('Content-Disposition''inline; filename="' $filename '"');
        $response->setHeader('Content-Transfer-Encoding''binary');
        $response->setHeader('Content-Length: 'filesize($file),'');
        $response->setHeader('Accept-Ranges:''bytes');
        readfile($file);
        unlink($file);
    }

Thank you work for me
Reply




Theme © iAndrew 2016 - Forum software by © MyBB