Protecting PDFs with a password | laravel-pdf-go | Spatie       

 SPATIE  

laravel-pdf-go
==============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-pdf-go](https://spatie.be/docs/laravel-pdf-go/v1)  Basic-usage  Protecting PDFs with a password

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-pdf-go/v1) 

  Protecting PDFs with a password    
- [ Introduction ](https://spatie.be/docs/laravel-pdf-go/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-pdf-go/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-pdf-go/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-pdf-go/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-pdf-go/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-pdf-go/v1/changelog)
- [ Alternatives ](https://spatie.be/docs/laravel-pdf-go/v1/alternatives)
- [ About us ](https://spatie.be/docs/laravel-pdf-go/v1/about-us)
- [ Testing ](https://spatie.be/docs/laravel-pdf-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-pdf-go/v1/releasing)

Basic usage
-----------

- [ Creating PDFs ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/creating-pdfs)
- [ Responding with PDFs ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/responding-with-pdfs)
- [ Formatting PDFs ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/formatting-pdfs)
- [ Saving PDFs to disks ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/saving-pdfs-to-disks)
- [ Attaching PDFs to mails ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/attaching-pdfs-to-mails)
- [ Queued PDF generation ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/queued-pdf-generation)
- [ Testing PDFs ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/testing-pdfs)
- [ Setting defaults ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/setting-defaults)
- [ Protecting PDFs with a password ](https://spatie.be/docs/laravel-pdf-go/v1/basic-usage/protecting-pdfs-with-a-password)

Drivers
-------

- [ Configuration ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/configuration)
- [ Customizing Browsershot ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/customizing-browsershot)
- [ Using the Cloudflare driver ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/using-the-cloudflare-driver)
- [ Using the DOMPDF driver ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/using-the-dompdf-driver)
- [ Generating PDFs on AWS Lambda ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/generating-pdfs-on-aws-lambda)
- [ Using the Chrome driver ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/using-the-chrome-driver)
- [ Using the Gotenberg driver ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/using-the-gotenberg-driver)
- [ Using the WeasyPrint driver ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/using-the-weasyprint-driver)
- [ Custom drivers ](https://spatie.be/docs/laravel-pdf-go/v1/drivers/custom-drivers)

Advanced usage
--------------

- [ Creating PDFs with multiple pages ](https://spatie.be/docs/laravel-pdf-go/v1/advanced-usage/creating-pdfs-with-multiple-pages)
- [ Using Tailwind ](https://spatie.be/docs/laravel-pdf-go/v1/advanced-usage/using-tailwind)
- [ Extending with Macros ](https://spatie.be/docs/laravel-pdf-go/v1/advanced-usage/extending-with-macros)
- [ Waiting for readiness ](https://spatie.be/docs/laravel-pdf-go/v1/advanced-usage/waiting-for-readiness)
- [ Caching PDFs ](https://spatie.be/docs/laravel-pdf-go/v1/advanced-usage/caching-pdfs)

 Protecting PDFs with a password
===============================

###  On this page 

1. [ Owner password and permissions ](#content-owner-password-and-permissions)
2. [ Supported drivers ](#content-supported-drivers)
3. [ Decrypting a PDF ](#content-decrypting-a-pdf)
4. [ Using a custom encrypter ](#content-using-a-custom-encrypter)

Call `Encrypt` to password-protect a PDF:

```
err := pdf.From(ctx).View("pdf/invoice", invoice).
    Encrypt(pdf.Encryption{UserPassword: password}).
    Save(ctx, "invoice.pdf")
```

Encryption runs after metadata and post-processing, using AES-256 revision 6. The default implementation is Go-native and handles compressed object streams. It does not need qpdf.

Owner password and permissions
--------------------------------------------------------------------------------------------------------------------------------------------------

```
b.Encrypt(pdf.Encryption{
    UserPassword: userPassword,
    OwnerPassword: ownerPassword,
    Permissions: []pdf.Permission{pdf.Print, pdf.Copy},
})
```

A nil `Permissions` slice grants all permissions. An empty slice grants none. Available permissions are `Annotate`, `Assemble`, `Copy`, `Extract`, `FillForms`, `Modify`, `Print` and `PrintHighResolution`. PDF viewers decide whether to enforce permissions. They are not a substitute for keeping the document private.

If no owner password is supplied, the encrypter generates a random one. An empty user password allows opening without a prompt while retaining owner permissions. Protect queue and cache backends because serialized jobs can contain the passwords and cached documents can contain personal data.

Supported drivers
-----------------------------------------------------------------------------------------------------------

Encryption works with all drivers, including custom drivers, because it runs after generation. A custom driver must return valid PDF bytes.

Decrypting a PDF
--------------------------------------------------------------------------------------------------------

```
contents, err := os.ReadFile("invoice.pdf")
if err != nil {
    return err
}
plain, err := pdf.From(ctx).Decrypt(ctx, contents, password)
```

Pass bytes explicitly. Unlike PHP, Go does not guess whether a string is a path or document contents. Incorrect passwords return an error. Either a user or owner password can decrypt the document.

Using a custom encrypter
--------------------------------------------------------------------------------------------------------------------------------

Set `Config.Encrypter` to your implementation of `pdf.Encrypter`. It provides `Encrypt(context.Context, []byte, pdf.Encryption)`, `Decrypt(context.Context, []byte, string)` and `Identity() string`. The first two return `([]byte, error)`.

The identity must change when behavior changes. It separates cache entries and lets queued jobs reject mismatched worker encryption. Your implementation is used for synchronous generation, queued generation and `Factory.Decrypt`.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
