CFDocument and CSS Hell

OK - how do I use a CSS stylesheet and CFDocument?

Right now I have several very simple .cfm pages - a print.css file and my print.cfm - which pulls in my various pages via cfinclude:

CODE:
  1. <cfdocument format="pdf">
  2.     <cfdocumentitem type="header">
  3.         <h4>Waste Pickup Request System Manual<h4>
  4.     </cfdocumentitem>
  5.  
  6.     <cfdocumentsection>
  7.                 <cfinclude template="createrequest.cfm">
  8.     </cfdocumentsection>
  9. </cfdocument>

This works and gives me my un-styled content.

OK. But where do I include my stylesheet?

I tried:

CODE:
  1. <link rel="stylesheet" href="print.css" type="text/css" media="print" />
  2. <cfdocument format="pdf">
  3. ...
  4. </cfdocument>

Doesn't work. Digging around in the live docs - someone mentions including it inside the cfdocument:

CODE:
  1. <cfdocument format="pdf">
  2. <link rel="stylesheet" href="print.css" type="text/css" media="print" />
  3. ...
  4. </cfdocument>

Nope. Doesn't work.

If I include it within each cddocument element - it works - but I get weird CSS scaling issues:

CODE:
  1. <cfdocument format="pdf">
  2.     <cfdocumentitem type="header">
  3.            <link rel="stylesheet" href="print.css" type="text/css" media="print" />
  4.            <h4>Waste Pickup Request System Manual<h4>
  5.     </cfdocumentitem>
  6.  
  7.     <cfdocumentsection>
  8.            <link rel="stylesheet" href="print.css" type="text/css" media="print" />
  9.            <cfinclude template="createrequest.cfm">
  10.     </cfdocumentsection>
  11. </cfdocument>

If I include the stylesheet withing my cfincluded page - it works -and I guess that is going to be my solution unless someone can tell me what I'm doing wrong here! :)

Update - August 17

My final solution - which seems to work OK... I include the stylesheet within the main cfdocument - and that seems to style the cfincluded templates. Then I call the stylesheet again within each cfdocumentitem to style the header and footer.

CODE:
  1. <cfdocument format="pdf">
  2.     <link rel="stylesheet" href="print.css" type="text/css" media="print" />
  3.  
  4.     <cfdocumentitem type="header">
  5.         <link rel="stylesheet" href="print.css" type="text/css" media="print" />
  6.         <h4>Manual: HSB Waste Pickup Request System</h4>
  7.     </cfdocumentitem>
  8.  
  9.     <cfdocumentitem type="footer">
  10.         <link rel="stylesheet" href="print.css" type="text/css" media="print" />
  11.         <cfoutput><p>PAGE: #cfdocument.currentpagenumber# of #cfdocument.totalpagecount#</p></cfoutput>
  12.     </cfdocumentitem>
  13.    
  14.     <cfinclude template="createrequest.cfm">
  15.         <cfdocumentitem type="pagebreak"/>
  16.     <cfinclude template="selectchemical.cfm">
  17.         <cfdocumentitem type="pagebreak"/>
  18.     <cfinclude template="checkout.cfm">
  19. </cfdocument>

Update - August 20
Tried Julian's suggestion (see comments below) of using the file protocol:

CODE:
  1. <cfdocument format="pdf">
  2.     <link rel="stylesheet" type="text/css" href="file:///#expandPath(".")#\print.css">

But oddly enough this didn't work at all - no styles were applied...

8 Comments

  1. Posted August 16, 2007 at 12:37 pm | Permalink

    What would be nice is if I could do:

    cfdocument stylesheet = “print.css”

  2. Posted August 16, 2007 at 1:21 pm | Permalink

    You’re not doing anything wrong there. What you’re doing is exactly what you should be :)

    An alternative is to have a generated HTML page and then use cfhttp to get that page and then use it to create the cfdocument. You might find this produces more reliable results and allows you to use a stylesheet

  3. Posted August 16, 2007 at 1:27 pm | Permalink

    The problem is these pages are just content - there is no HTML header/footer, etc. I could maybe generate a printer friendly index.cfm and call that with CFDcoument - but that seems like extra work I shouldn’t have to do.

  4. Posted August 16, 2007 at 10:27 pm | Permalink

    agreed. Are you perchance running CF8? I found cfdocument far better with CF8 than 7, even with existing apps.

  5. Posted August 16, 2007 at 11:10 pm | Permalink

    First off Jim is correct, you need to have a fully valid HTML page in order to get cfdocument to even remotely render correctly.

    I would recommend (and what I personally do) is create your page statically first with some dummy data and then validate it against the w3c validator. Make sure your using XHTML 1.0 Transitional, that seems it to work best.

    The final step is DO NOT use link on inline styles within your cfdocument, you MUST use import to load your css. This was mentioned to me a long time ago and I’ve always remembered it. It’s saved me a bunch of time and frustration.

  6. Posted August 17, 2007 at 8:55 am | Permalink

    Not running CF8 (yet)… Tony - I’ll try using an import vs. a link to call my CSS. I’ve updated the post above with my final solution - and it seems to work OK. And FWIW - none of my cfincluded documents include any doctype, head/body, etc…

  7. Posted August 17, 2007 at 10:01 am | Permalink

    OK - FWIW - I get the same behavior with @import. I still have to include it within each cfdocumentitem, etc - no different than using link…

  8. Posted August 20, 2007 at 3:52 am | Permalink

    Jim, try using the file protocol with a file system path to the stylesheet:

    [link rel=”stylesheet” type=”text/css” href=”file:///#expandPath(myCssDirectory)#docstyles.css”]

    Note the 3 slashes, not 2. This is vital.

    Also, my understanding is that only html 4.0 is supported in cfdocument (ie not xhtml).

Creative Commons License

Creative Commons License This article: CFDocument and CSS Hell, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License .

Copyright © 2007 thecrumb.com. All rights reserved.