Search code examples
angularlaravelpdfpdfmakemaatwebsite-excel

How to add security properties of pdf while generating PDF from HTML using javascript?


I am developing an Angular 4 project with PHP/ Laravel backend.

There are two ways I can generate PDF: - 1. Using pdfMake at frontend 2. Using dompdf or maatwebsite or any other laravel library by sending request to backend

Both of the above ways are enough to generate a plain PDF.

The problem is I want to add some security feature of PDF before the file gets generated viz. No content copying, No Print,page extraction, etc. Thus when the pdf is downloaded by the user, these features should be implemented in that particular pdf.

Appreciate any help.


Solution

  • Sovled the issue with laravel PHP.

    reference link:- https://mpdf.github.io/reference/mpdf-functions/setprotection.html

    Also, https://packagist.org/packages/niklasravnsborg/laravel-pdf?q=&p=2

    1. composer require niklasravnsborg/laravel-pdf
    2. go to config/app.php and add below lines

      'providers' => [ // ... niklasravnsborg\LaravelPdf\PdfServiceProvider::class ] 'aliases' => [ // ... 'PDF' => niklasravnsborg\LaravelPdf\Facades\Pdf::class ]

    3. Run this command from terminal - php artisan vendor:publish

    4. Basic pdf generation-- code in PHP

      use PDF; function generate_pdf() { $data = [ 'foo' => 'bar' ]; $pdf = PDF::loadView('pdf.document', $data); return $pdf->stream('document.pdf'); }

    // other opptions are save($filename), download($filename), output() 5. set protection using

    use PDF;
    function generate_pdf() {
        $data = [
            'foo' => 'bar'
        ];
        $pdf = PDF::loadView('pdf.document', $data);
        $pdf->SetProtection(['copy', 'print'], '', 'pass');
        return $pdf->stream('document.pdf');
    }
    

    // pass blank array in set protection instead of copy and print to deny all permissions to users