How to fix "Exit with code 1 due to network error: ProtocolUnknownError" with wkhtmltopdf

Posted . Visible to the public.

New versions of wkhtmltopdf dissallow file:// URLs by default. You can allow them by passing --enable-local-file-access.

If you are using PDFKit, set the option

PDFKit.configure do |config|
  config.default_options = {
    enable_local_file_access: true,
  }
end

This will be necessary in many setups to allow wkhtmltopdf to fetch assets (such as stylesheets) from the filesystem.

Note on security

Allowing this poses some risk when you render user input, since it might be feasible to include data from the local filesystem into the PDF. Therefore, the usual XSS protection techniques should be used.

It might be worth considering whether you can go without local file access and load assets via http://localhost or similar.

Also note that older versions of wkthmltopdf allow file:// by default.

Tobias Kraze
Last edit
Tobias Kraze
License
Source code in this card is licensed under the MIT License.
Posted by Tobias Kraze to makandra dev (2022-09-20 14:52)