Dompdf - Block printing and download

Asked

Viewed 1,457 times

2

You can block printing and download in Dompdf?

  • Hello Rafael, the answer helped you, there is some doubt about her?

1 answer

2

There is no way to prevent download definitively, what you can do is at most hinder, as the reply on Soen and in the Grupo Google

You can use the CPDF to specify what the user can do with the document.

https://github.com/dompdf/dompdf/blob/master/lib/Cpdf.php

According to CPDF documentation:

Calling the function setEncryption() you will be able to make the file encrypted, the user will not be able to use copy, modify or print (not least in the most used PDF readers, such as Adobe Reader, Foxit and the native Chrome)

$cpdf = $dompdf->get_canvas()->get_cpdf();
$cpdf->setEncryption('', '', array('copy', 'print'));

If you want to set up:

$cpdf = $dompdf->get_canvas()->get_cpdf();
$cpdf->setEncryption('usuário', 'senha', array('copy', 'print'));

It is not possible to block the download, everything we navigate even if the download itself does not occur has actually been downloaded to your machine at how much rendering or before rendering, maybe you have problem with automatic download, This is standard to disable you can adjust the parameters using 'Attachment' => 0, thus:

  • Version 0.7

    <?php
    use Dompdf\Dompdf;
    
    require 'vendor/autoload.php';
    
    $dompdf = new Dompdf();
    $dompdf->loadHtml('<strong>Stack</strong> Overflow');
    
    $dompdf->render();
    
    $dompdf->stream('document.pdf', array( 'Attachment' => 0 ));
    
    //Pra prevenir espaçamentos no final
    exit;
    
  • Version 0.6 or older

    <?php
    
    require_once 'dompdf_config.inc.php';
    
    $dompdf = new Dompdf();
    $dompdf->load_html('<strong>Stack</strong> Overflow');
    
    $dompdf->render();
    
    $dompdf->stream('document.pdf', array( 'Attachment' => 0 ));
    
    //Pra prevenir espaçamentos no final
    exit;
    

Still if your problem is Ctrl+S keyboard there is not much to do, at most to try to avoid I would say that the recommended to just make it difficult would be to create a page with PDF using iframe and hash #toolbar=0 for example:

<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
    *, html, body, .pdf {
        width: 100%;
        height: 100%;
        overflow: hidden;
        border: none;
        margin: 0;
        padding: 0;
    }
    </style>
</head>
<body>
    <iframe src="pdf.pdf#toolbar=0" class="pdf"></iframe>
</body>
</html>

This will disable Toolbar, which helps a bit, not least in the most used PDF readers like Adobe Reader, Foxit and the Chrome native you said earlier.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.