How to force a PDF to be displayed in the browser

7

I have a sequence of PDF files that I can link to normally, but as soon as I click on one of them it downloads, and I need the PDF to be displayed in the browser itself. I've tried using this in my link but it did not work:

target="_blank"

I can not let the user download directly in any way. How do I resolve this issue?

    
asked by anonymous 17.04.2015 / 13:19

3 answers

3

As already mentioned by Ivan Nack, this will depend on the browser, there is how to manipulate the header header / em> Content-Disposition , however it will depend on how the browser will handle the file, because there is a way to configure the browser to always download the file, or if the browser does not handle native PDFs, use plugins such as Adobe Reader.

As stated by the bfavaretto in comment , one way to do this is to convert each page of the PDF file into an image and display it, this is how Google should do ( I believe ), this in PHP is using ImageMagick .

Here is a sample taken from here :

$arquivoPDF = 'demo.pdf';
$imagem = 'demo.jpg';
$img = new imagick();

$img->setResolution(200,200); // Isto é importante para dar uma saída de boa qualidade, caso contrário, o texto pode não ser claro  
$img->readImage("{$arquivoPDF}[0]"); // Lê a primeira página do PDF, o número entre [] indica a página
$img->scaleImage(800, 0);            // Reduz as dimensões
$img->setImageFormat('jpg');         // Define novo formato
$img = $img->flattenImages();        // Isso é necessário para imagens com transparência, que irá produzir um fundo branco para regiões transparentes
$img->writeImages($imagem, false);   // Salva a imagem

Another alternative, now in Javascript, is to use a renderer such as pdf.js >, which does not require third-party applications. There's a demo here .

Finally, there's also how to use the Google viewer to do this what you want, for example:

<iframe src="http://docs.google.com/gview?url=http://infolab.stanford.edu/pub/papers/google.pdf&embedded=true"
 style="width:600px; height:500px;" frameborder="0">
</iframe>

DEMO

    
17.04.2015 / 17:31
3

As far as I know how PDFs are not native web pages it is the browser that decides what to do with them. So the browser is that it has to be configured to open using some plugin or the internal viewer like in firefox for example.

Or try this

header("Content-Type: application/pdf");
header('Content-Disposition: inline; "nome_do_arquivo.pdf"');
    
17.04.2015 / 16:00
0

I was with a similar difficulty following the solution I found giving the two options view in the browser or Download:

<?php
function varSet($VAR) { return isset($_GET[$VAR]) ? $_GET[$VAR] : ""; }
$action = varSet("action");
$pasta = base64_decode(varSet('pasta'));

//Lista dos arquivos que nao serão listados
$denyFiles = array(".htaccess","thumbs.db");

if ($action == "download") {
    $file = base64_decode(varSet("file"));
        header("Content-disposition: attachment; filename=\"".basename($file)."\"");
    readfile(".$file");
    exit;
}
 if ($action == "embed") {
    $file = base64_decode(varSet("file"));
        header("Content-Type: application/pdf");
    readfile(".$file");
    exit;
}
?>
<?php
$pasta = '/arquivos';
$arquivos = "$user->cod_func".'  '.utf8_decode($_POST['select_mes']).' de '.$_POST['select_ano'].'.pdf';
$filename = 'arquivos/'.$arquivos;


if (file_exists($filename)) {
?>  
Download do Arquivo: <a href="?action=download&file=<?php echo base64_encode("$pasta/$arquivos"); ?>"><?php echo $_POST['select_mes'].' '.$_POST['select_ano']; ?></a>
<br><br>
Vizualizar: <a href="?action=embed&file=<?php echo base64_encode("$pasta/$arquivos"); ?>"><?php echo $_POST['select_mes'].' '.$_POST['select_ano']; ?></a>
<br>

<?php
} else {
    echo "Não existe holerith no mês selecionado";
}
?>
    
10.11.2016 / 13:12