Skip to content

Instantly share code, notes, and snippets.

@jakejackson1
Last active March 31, 2023 04:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakejackson1/2822bd311df289684840 to your computer and use it in GitHub Desktop.
Save jakejackson1/2822bd311df289684840 to your computer and use it in GitHub Desktop.
A simple Hello World template for Gravity PDF with additional styling
<?php
/**
* Template Name: Hello World
* Version: 0.1
* Description: A basic "Hello World" PDF template showing custom PDF templates in action
* Author: Jake Jackson
* Author URI: https://gravitypdf.com
* Group: Sol System
* License: GPLv2
* Required PDF Version: 4.0
* Tags: space, solar system, getting started
*/
/* Prevent direct access to the template (always good to include this) */
if ( ! class_exists( 'GFForms' ) ) {
return;
}
/**
* All Gravity PDF v4/v5/v6 templates have access to the following variables:
*
* @var array $form The current Gravity Form array
* @var array $entry The raw entry data
* @var array $form_data The processed entry data stored in an array
* @var object $settings The current PDF configuration
* @var array $fields An array of Gravity Form fields which can be accessed with their ID number
* @var array $config The initialised template config class – eg. /config/zadani.php
*/
?>
<!-- Any PDF CSS styles can be placed in the style tag below -->
<style>
h1 {
text-align: center;
text-transform: uppercase;
color: #a62828;
border-bottom: 1px solid #999;
}
</style>
<h1>Hello World</h1>
@jeffreywdonahue
Copy link

any PDF CSS is a bit of a stretch for current css standards. Grid definitely not adopted. uses mPDF to write the PDF file.

@jakejackson1
Copy link
Author

jakejackson1 commented Nov 16, 2020

@jeffreywdonahue

any PDF CSS is a bit of a stretch for current css standards. Grid definitely not adopted. uses mPDF to write the PDF file.

Correct, modern CSS like Grid of Flexbox is not supported in mPDF. The comment block on line 22 specifically references PDF CSS styles, and this private Gist is only linked to on a page in our documentation that includes additional context/links about limitations of the PDF engine. When looked at in that regard "PDF CSS" shouldn't be misleading/suggest modern CSS is 100% supported, but I thank you for the feedback.

@jeffreywdonahue
Copy link

jeffreywdonahue commented Nov 16, 2020 via email

@jakejackson1
Copy link
Author

Yes, I'm one of Gravity PDF's developer. Floats or tables (yeah, I know...) is how you'd need to do layout in your PDF templates.

@jeffreywdonahue
Copy link

jeffreywdonahue commented Nov 18, 2020 via email

@jakejackson1
Copy link
Author

No stress at all! Like you, I'd love for an open source PHP HTML to PDF library that supported modern CSS. Maybe one day...

@drozumek
Copy link

Is there any update for developers about how to create PDF? I mean modern CSS etc? Or still floats and tables are neccessary to use?

@jakejackson1
Copy link
Author

@drozumek nope. Still tables and floats. The underlying tech Gravity PDF uses is the open source library mPDF and contributions are welcome!

@pheraph
Copy link

pheraph commented Dec 15, 2021

I am still learning how to create my own template. Do you have an idea for a nice development workflow to see the effects of changes to my custom template directly in a PDF? Do I have to save a form every time and generate it as a PDF or is there a more elegant way to do this (maybe Wordpress CLI)?

@jakejackson1
Copy link
Author

@pheraph we've found the best development workflow is to have WordPress running locally on your computer (using Local or a similar tool). You can then make live changes to the PDF Template and see the results with a simple refresh of your browser.

@pheraph
Copy link

pheraph commented Jan 6, 2022

When asked I didn't know the PDF file is regenerated on a tab reload with the updated template. In conjunction with the HTML-view, the browsers object inspector and some of the template hooks it was really easy and fun. Thanks!

@jasimp
Copy link

jasimp commented Feb 28, 2023

Hi
how to make auto page break with this template?
also how to set content area (body, header & footer)

@WebEngineerGuy
Copy link

WebEngineerGuy commented Mar 16, 2023

Hello,

When Gravity PDF plugin has an update, will any edits we make on the core templates be overwritten by the update? Will the templates we create also be overwritten or deleted due to a Gravity PDF plugin update?

Thanks.

@jakejackson1
Copy link
Author

Hi how to make auto page break with this template? also how to set content area (body, header & footer)

@jasimp sorry for the delay. I've been out of the office and only have just returned.

See our documentation for how to control the Header / Footer content of a PDF: https://docs.gravitypdf.com/v6/developers/pdf-features/headers-and-footers

The body content of the PDF will be whatever standard HTML you output in your custom template.

Page breaks will automatically occur once the body content reaches the bottom of the page content area. You can also add manual page breaks if needed: https://docs.gravitypdf.com/v6/developers/pdf-features/pagebreaks

@jakejackson1
Copy link
Author

Hello,

When Gravity PDF plugin has an update, will any edits we make on the core templates be overwritten by the update? Will the templates we create also be overwritten or deleted due to a Gravity PDF plugin update?

Thanks.

@WebEngineerGuy If you follow our development guide for modifying core templates, then no. Your changes won't be overridden or deleted during a plugin update: https://docs.gravitypdf.com/v6/developers/template-hierarchy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment