How to open pdf file on button click in php

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    PHP uses a standard code to display the pdf file in web browser. The process of displaying pdf involves location of the PDF file on the server and it uses various types of headers to define content composition in form of type, Disposition, Transfer-Encoding etc. PHP passes the PDF files to read it on the browser. Browser either shows it or download it from localhost server then display pdf.

    Note: PHP is not actually reading the PDF file. It does not recognize File as pdf. It only passes the PDF file to the browser to be read there. If copy the pdf file inside htdocs folder of XAMPP then it does not need to specify the file path.

    Example 1: This example display the pdf file on the browser.

    $file = 'filename.pdf';

    $filename = 'filename.pdf';

    header('Content-type: application/pdf');

    header('Content-Disposition: inline; filename="' . $filename . '"');

    header('Content-Transfer-Encoding: binary');

    header('Accept-Ranges: bytes');

    @readfile($file);

    ?>

    Output:

    How to open pdf file on button click in php

    Example 2:
    this examples displays a format and explains every section of code

    $filename = "/path/to/the/file.pdf";

    header("Content-type: application/pdf");

    header("Content-Length: " . filesize($filename));

    readfile($filename);

    ?> 

    Output:

    How to open pdf file on button click in php

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    You have one to many quotes on this line

    $pdf->Output('$rand.'.pdf','F');
    

    Try this

    $pdf->Output($rand . '.pdf', 'F');
    

    Also as you are setting a click handler I would not run it in a function, run it without the function wrapper to make sure it is run.

    
    

    And you dont appear to run the PHP Function, so either add a call or dont put that code in a function

    AddPage();
            $pdf->SetFont('Arial','B',16);
            $pdf->Cell(40,10,'Hello World!');
        
            $rand = rand(1, 1000);
        
            $pdf->Output($rand . '.pdf', 'F');
        }
    }
    
    download();
    

    Personally I would do it without the function wrapper as

    require (get_template_directory() . '/assets/php/fpdf/fpdf.php');
    
    if (isset($_POST['download'])){
    
        $pdf = new FPDF();
        $pdf->AddPage();
        $pdf->SetFont('Arial','B',16);
        $pdf->Cell(40,10,'Hello World!');
    
        $rand = rand(1, 1000);
    
        $pdf->Output($rand . '.pdf', 'F');
    }
    

    Because of LAST Comment

    Then you need to pass the download parameter on yrou ajax call

    
    

    open

    Generate PDF and download PDF on button click using PHP DomPDF.

    Video time control bar 0:00

    Audio volume control bar 0:00 / 0:00 Quality:

    Sharing buttons:

    Can we read PDF file in PHP?

    Note: PHP is not actually reading the PDF file. It does not recognize File as pdf. It only passes the PDF file to the browser to be read there.

    How can I open PDF file without downloading in PHP?

    Under "Privacy and security," click Content settings. Near the bottom, click PDF documents. Turn off Download PDF files instead of automatically opening them in Chrome. Click on Extreme Right 3 lines.

    How do I open PHP files in PDF?

    PHP to PDF.
    Open your PHP file with your standard application on your computer as usual..
    There go to File -> Print or just press. Ctrl. + P. ... .
    Choose "Microsoft XPS Document Writer" as your printer..
    Click on "OK" or "Print"..
    Select a destination for your XPS file and click on "Save"..