March 15, 2023
External CSS in cfdocument
Comments
(3)
March 15, 2023
External CSS in cfdocument
Newbie 1 posts
Followers: 0 people
(3)

How to use external CSS while generating a .pdf using cfdocument.
Tried many things I found on the internet but without success.  Simple Div formatting, fonts etc doesn’t work.  If I use Inline CSS it works fine, not with internal or external CSS.  Tried :

1) <link rel=’stylesheet’ type=’text/css’ src=’#expandPath(“/iSupport/css/”)#FedEx.css’>

2) @Import

3) Include…

4) changed .css to .cfm

3 Comments
2023-03-20 19:28:39
2023-03-20 19:28:39

Something else to consider….

To my knowledge, CFDocument only supports HTML4 and its CSS support is very sparse.

Consider using CFHTMLTOPDF

Also, I’ve found the fonts won’t work unless you’ve actually installed the fonts in the CFAdmin: Font Management.  Sometimes, even then not all fonts would work.

Like
2023-03-20 17:41:15
2023-03-20 17:41:15

One other thing you can try: add the site’s domain to your server’s hosts file, using the loopback address. In other words:

127.0.0.1 my-cf-site.net

(of course, replace “my-cf-site.net” with your actual domain)

I’ve found this helps on sites where the domain doesn’t have a public DNS listing, like a Development or Testing server.

It’s been a while since I figured this out, so I don’t remember how this fix interacts with the localurl attribute that Charlie pointed out. Probably start with that, and then add this to the mix if localurl alone doesn’t cut it.

Like
2023-03-16 22:57:16
2023-03-16 22:57:16

When you try to use ANY static files within HTML in a cfdocument (like css or images or js files), something to beware is that CF has (since CF6) internally turned that request into the equivalent of a cfhttp call “out” to the server where your CF page is running, to “get” that file–even though we’d argue “it’s right there!”.

And the problem is that sometimes that internal cfhttp call will fail, for any number of reasons…but it will be silent. You will see no error, just that the thing you tried to point to doesn’t appear/work. (In years past people talked about getting a “red x” on their images.)

So to solve that, they added in CF8 a new  attribute for cfdocument: localurl=”true”

Can you add that and see if it solves your problem?  This is one of those esoteric/annoying edge cases that most are not aware of. Hope it’s the solution for you. FWIW, I have a blog post from 2011 that discussed the issue, albeit from another performance negative that could arise from the problem, but the info is just as valuable today and appropriate to a situation like yours, if I’ve guessed things right.

Let us know how it goes.

Like
Add Comment