Home > Mobile >  Read pdf without download and print it with html php
Read pdf without download and print it with html php

Time:04-13

Good evening, I would like to create a feature for my users that allows them to read a pdf file without downloading or printing it. They will only be able to read the pdf file. I tried pdf.js which did not work, I also tried to convert my pdf to html via a script, I also tried this php code but the download and print button appears:

    <?php

// The location of the PDF file
// on the server
$filename = "/path/to/the/file.pdf";

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

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

// Send the file to the browser.
readfile($filename);
?>

Do you have any solutions or technologies to suggest to me, I use PHP, HTML and JS.

CodePudding user response:

Try this one:

<?php

// Store the file name into variable
$file = 'filename.pdf';
$filename = 'filename.pdf';

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

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

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

header('Accept-Ranges: bytes');

// Read the file
@readfile($file);

?>

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.

CodePudding user response:

The php script you offered only can download the file. if you use php ,you can try the demo ,which is using pdf2text here or try the method in this link read-pdf-files-with-php

  • Related