Rip's Domain

CFDOCUMENT. It works if you know how.

Posted in ColdFusion by rip747 on September 10, 2007

Many people have tried and few have succeeded using CFDOCUMENT to print their HTML.

For those that don’t know, here is how I’ve tamed the beast numerous times. Using these techniques you should be able to get CFDOCUMENT to print your HTML perfectly.

1) Your page must be valid XHTML 1.0 Transitional. Once you have created your HTML, run it against W3C’s validator and make sure it validates. So make sure to start your HTML with the following:

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;

2) Use @import to include your CSS. Don’t use the link tag, inline styles or a style block. It must be included with @import.

<style type=”text/css” media=”screen”>@import “style.css”;</style>

That’s it!!! How easy is that. I can bet you that just by doing these two things you can eleviate many of the headaches that you have getting CFDOCUMENT to print properly.

* Remember that border-collapse doesn’t work and there is currently no fix for it.

About these ads

21 Responses

Subscribe to comments with RSS.

  1. Johan said, on September 11, 2007 at 3:35 am

    I believe the main requirement is that is is a valid XHTML page and that the print media stylesheet is valid. I did not find the xml declaration or using import for stylsheets was necessary.

    I have had problems with page breaks – images, lines of text (pdfs mainly) split over two pages. I believe the issue is with the print stylesheet more than anything else.

    See below – same content dynamically generated in different formats

    http://www.assetnow.com/index.cfm/1,82,252,0,html

    http://www.assetnow.com/index.cfm/1,82,252,0,pdf

    http://www.assetnow.com/index.cfm/1,82,252,0,mfp

  2. Kathleen said, on October 19, 2007 at 12:49 pm

    Your pagebreak problems could be a result of your margins declared within the tag, and the amount of space needed vs the allocated there for your section items. The ‘margintop’ number allocates space for your header, same goes for ‘marginbottom’ for the footer. Everything in between will break up into however many pages is necessary to display it.

    What I can’t figure out is, if I create more than one multiple-page document, (via cfoutput query or cfloop within the cfdocument tag) how do I get the page number to restart at one? (I can number a single multiple-page document with no problem, of course.)

  3. rip747 said, on October 19, 2007 at 1:01 pm

    @kathleen,

    Thanks for the tips!

    don’t know about the page numbering problem though. :(

  4. Rusty Owens said, on October 25, 2007 at 10:01 am

    What about this? I am trying to include a bar code graphic on the page. If I create the pdf, you may or may not see the graphic and it won’t print at all. The only time you see it on the screen is when you zoom in.

  5. Jorrt said, on November 6, 2007 at 10:21 am

    The thing with barcodes is that you have to embed the font. You can do this in the cfdocument tag (fontembed=”Yes”). Remember that you then have to use inline styles: <span style=”font-family: IDAutomationHC39M;font-weight: normal;font-size:28px;”>*12345*</span>
    Remember that some barcode fonts only work at specific font sizes (read the manual of the barcode font)

    I did not find it necessary to use @import to include CSS, a simple style tag with the styles inside work just as well.
    Also, there is no need to declare the XML, just be sure that the contents is strict XHTML.

    Another thing: The top margin tends to behave a bit different than expected. If you set the margintop at a specific height, but the contents of the header is bigger, it will overlap the contents of the pdf.

  6. Brian K said, on December 2, 2007 at 4:27 pm

    Finally found this article after hours of frustration. Simply adding the proper page headers allows the PDF to generate. Thanks for your help and placing this article out into the world.

  7. Gary F said, on February 18, 2008 at 7:54 pm

    I upgraded our test server to CF8 and our generated PDFs became a mess. We have random spaces between words, bad text alignment and random invisible text (white on white). Worse still, the PDF file size has tripled!

    Even when using valid XHTML (as verified by validator.w3.org) complete with declarations the problems persist.

    Regarding the bloated PDF file size, we use fontembed=”yes” because without it all of the text looks too bizarre to read. We’re just using Arial as well which is odd. In CF7 we used fontembed=”yes” and the file size was still 1/3 of what it is in CF8.

    All of these significant changes to CF8 should have been documented by Adobe including a section on “how to do it so that it works”. Argh!

    Does anyone have some more pointers that could help please? Thanks.

    Gary.

  8. Rick Harris said, on February 28, 2008 at 4:13 pm

    I have tried these steps. The outcome was still the same for me. CFDOCUMENT still renders the pages about 30 percent smaller than what CFDOCUMENT did in CFMX7.

  9. rampage said, on March 11, 2008 at 6:47 am

    Same problem here!
    Thanks for the tip with the document type.But it dont really solved the size problem. Also installed the newest hot fix and tried with various doctypes.
    By the way, the file sizes are also different: CF7 PDFs are ca. 70 – 100kb, while CF8 generates 550-600kb files! Is there probably a problem with dpi resolution?
    I would go back to CF7, when theres no sexy cfthread tag in CF8…

    Any ideas?Thanks

  10. rip747 said, on March 11, 2008 at 12:01 pm

    @rampage

    i would suggest filing a bug with Adobe. make sure that went you do, you include the code and the result for both cf7 and cf8.

  11. Sean Mitchell said, on March 22, 2008 at 12:31 pm

    I am trying to use the cfdocument tag to create a dynamically generated pdf. For some reason, anything inside of the tag will not show on the pdf, its like it just skips the output variable…any thoughts?
    Thanks!

  12. Dave Phipps said, on March 31, 2008 at 3:35 am

    @Sean

    I am seeing the same thing. If I do a very basic cfdocument with just a single line which outputs the date then it works. As soon as I try and include anything more complicated (not really complicated just a bunch of paragraphs of text with a few variables) I get a blank pdf! My more complex page is valid xhtml and the css is also valid, whereas my demo page is literally:

    #Now()#</cfoutput

    and this works! Any ideas?

  13. Sebastiaan said, on November 17, 2008 at 9:44 am

    Hi all,

    I’ve used cddocument for a long time now on CFMX7 – and using an inline styleblock with valid html 4.01 strict works like a charm ;-)

    Haven’t tried the code on CF8 though!

    I’ve set permissions tot AllowPrinting,AllowCopy. But when orientation is set to Landscape I can only copy text fromt he current page in the Adobe Reader browser-plugin (Opera, Firefox, IE). Orientation default is no problem. Any ideas?

  14. […]  This threw me for a total loop.  I wasn’t sure what had happened.  I found this post – cfdocument post by rip – and thought that may be the solution, but it wasn’t working either.  I got the pages to be […]

  15. Jason Haritou said, on April 28, 2009 at 10:17 am

    I had a problem with embedding the fonts with CF and cfdocument. I registered the fonts with CFadministrator, made sure I was using XHTML transitional, tried using inline styles, @stylesheet, fontembed=true, false and everything else inbetween.

    What fixed it for me was moving the font files into the documentroot for the website. Be aware though, that this means by default your font files will be available to download to anyone who wants them.

  16. Phillip Senn said, on August 3, 2010 at 12:30 pm

    Your line
    ?xml version=”1.0″ encoding=”UTF-8″?

    has some weird quote marks in it.
    Also, I wonder if
    @import “style.css”;
    needs to be
    @import url(“style.css”);

  17. Phillip Senn said, on August 3, 2010 at 1:13 pm

    I think a title tag has to be in the head section in order to pass validation as well.

  18. hinsel said, on August 7, 2010 at 1:35 am

    UGH!
    what a nightmare. had to go back to using cfdocument + cfdocument section to generate a pdf from a user entered form when i found out the client got CF7 after i had coded it beeeautifully using cfpdf in cf8 (which actually lets you populate the form fields rather than just create a poopy flat pdf!)

    anyhow…i’ve got some textareas that i simply can NOT get the borders on to disappear. have tried border:none inline and in style tag, tried individually coding border-bottom etc even things like trying to set the color to white so they don’t look like they are there. damn thing just ignores it, renders the border regardless of what i tell it…any ideas???

  19. Doug said, on January 25, 2011 at 3:34 pm

    My problem is that I want to control the font color within my form fields, and I got no response.

    I cleaned up my doc so that it’s XHTML compatible, imported my style sheet.
    When I view it on screen, everything works fine, when I throw my cfdocument tags into place… nothing happens..

    Any ideas?

  20. hinsel said, on January 28, 2011 at 2:50 pm

    Doug,

    You want to control the font color on the HTML part of the form or on the final printed pdf?

    Maybe it doesn’t like the imported CSS ?
    Have you tried inline styling for the form fields?

    I know that should change the color on the html form the user sees, not sure if it will carry over to the pdf itself when it’s created…

    Hope that helps!

  21. Matt said, on May 4, 2011 at 3:05 pm

    When you create a PDF using cfdocument, the tag and tag renders incorrectly sometimes. When it’s at the edge of the table, the text inside the tags is repeated twice. And sometimes, it truncates the space or spaces before the tag. Or it would overlap the tagged text over the text before the tag. Has anyone encountered any of these problems?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: