Inline styling of PDF with variables

Hi there,
we came from this discussion, that talks about styling of Websites [DONE] Internal CSS Deleted on Source Code but have a different challenge.

We would like to style PDFs with variable styles. For that we load the complete style description (“style=…”) / style itself (“background-color: #e7304b; margin: 0; padding: 15px 50px 8px 50px;”) into a variable and try to “inject” them into the source code of the PDF. However - the code will always be stripped or changed within the <div> tag.

Is there any valid way to get the styling from a variable into the PDF Source code? We even tried with the complete opening DIV tag, but failed.

tried with field reference:
Bildschirmfoto 2025-02-07 um 10.42.22

tried with complete style information:
Bildschirmfoto 2025-02-07 um 10.39.12

Thanks for your help!

Hey @dirk_s

You won’t be able to add the CSS code directly to the source code. You need to execute a script with the CSS and then add that variable in the PDF document. It will look like this:

  1. Create the CSS variable and add <style></style> inside a backtick (template literal). Also add the variable @css at the end of the PDF document.

  1. In the PDF source code now you can reference the custom css classes by adding them inside <div> or any other html tag.

  1. Now when you click “Simulate Variable” you can see the CSS design.

Although this works, the source code will get messy and very difficult to handle in the Tape PDF editor. I suggest you use an external HTML/CSS editor and then copy/paste the CSS to the variable and the HTML to the source code.

Another thing you could do instead is use inline CSS in the source code. The good thing about inline CSS is that you will be able to see the design without having to simulate variables.

2 Likes

Thanks @Luis - thats an interesting solution!

We also tried later with preparing the whole body in an execute script field, which can then contain other variables and links to fields. We then only post the body variable into the PDF action. Its limited by the size of the variable.

But your approach seems to be a bit smarter - we didn’t try to add the css at the end so far.
Thanks!

If I remember correctly, this is a limitation of TinyMCE. We had the same workaround as @Luis in Tape and the previous platform.

We took it a step further and build out every using calc fields, them we put all of them into another calc before putting it into the editor itself. This eliminated all the empty tags that would populate in the HTML.

CSS calc = CSS
HTML calc = HTML

Body calc = CSS and HTML calcs

Add token to editor.

The approach with CSS in a variable works, but does not immediately solve all the problems: the PDF converter behind tape obviously cannot handle hyphenation, does anyone also have an approach for this?

One thing you could do is add hyphens: auto; in the CSS body.