Skip to content

WordPress plugin: adds a URL suffix which allows the site visitor to download the selected Post as a PDF.

Notifications You must be signed in to change notification settings

SayHelloGmbH/hello-post-to-pdf

Repository files navigation

Post To PDF

Description

Adds a URL suffix which allows the site visitor to download the selected Post as a PDF.

PDF generation occurs using an HTML document formatted with CSS, using Michael Härtl's PHP code and the wkhtmltopdf command line tool.

Usage

  • Install the plugin. This will flush the rewrite hooks, so that the custom rewrite endpoint shpdf becomes available.
  • Call your Post using the additional URL suffix shpdf/ (e.g. https://example.com/2019/03/my-post/shpdf/).
  • The plugin contains a default, very simple PDF template. You can override it simply by adding your own template to your Theme or by passing an array of possible template paths.

Allowed post types

By default, only Posts of type post and page are permitted. To allow rendering of other Post Types, use the following hook in your Theme.

add_filter('hello-post-to-pdf/allowed-post-types', function(array $allowed_post_types){
	$allowed_post_types[] = 'custom-post-type';
	return $allowed_post_types;
}, 10, 1);

Template override: predefined file

If a file named single-hello-post-to-pdf.php exists in the root folder of your Theme, this will be used in preference over the plugin's own template.

Template override: hook

The best way to override the rendering with your own template is through the use of the following hook in your Theme.

You need to always return an array of file paths which are relative to the Theme root folder. Even if you want to use one specific template.

Example:

add_filter('hello-post-to-pdf/theme-templates', function(array $template_paths){
	$template_paths = [
		'templates/' .get_post_type($post_id). 'post-to-pdf.php',
		'templates/post-to-pdf.php'
	]
	return $template_paths;
}, 10, 1);

Modifying the PDF generation wrapper options

You can modify the standard set of PDF wrapper options by using the hello-post-to-pdf/generation-settings hook.

add_filter('hello-post-to-pdf/generation-settings', function(array $settings){
	return array_merge($settings, [
		'margin-top' => 20,
		'margin-bottom' => 20
	]);
}, 10, 1);

Changelog

2.0.1

  • Fix security warning advised by Github.

2.0.0

  • Improves previous code.
  • Adds safety checks.
  • Adds hello-post-to-pdf/generation-settings filter.
  • Extends README and provides licensing information.

1.1.0

  • Fixes for Coding Standards.
  • More complete config example.
  • Adds language suffix to filename if WPML is active.

1.0.1

  • Fix allowed post types sequencing.

1.0.0

  • Add simple template.
  • Add hooks.
  • Add PDF rendering.
  • Add usage instructions.

0.0.1

  • Initial development version.

Contributors

License

  • phpwkhtmltopdf by Mike Härtl is provided under the terms of the MIT license.
  • The wkhtmltopdf command line tool is provided under the terms of the GPL v3 license.
  • The remaining code in the plugin is provided under the terms of the GPL v3 license.

Use of this code provides and implies no guarantee.

About

WordPress plugin: adds a URL suffix which allows the site visitor to download the selected Post as a PDF.

Resources

Stars

Watchers

Forks

Packages

No packages published