Skip to main content
New Participant
June 2, 2022
Question

cfprint shows blank page in coldfusion

  • June 2, 2022
  • 2 replies
  • 888 views

I am generating a pdf document and on clicking print button , print window should appear, but my page returns blank. Why this happens. 

What I have did

 <cfoutput><a href="print.cfm?user_id=#session.sessionUser.user_id#"><i class="fa fa-print"></i></a></cfoutput>

print.cfm

<cfparam  name="user_id" default="v">
<cfset doc_data=createObject("component","components.results")>
<cfset data=doc_data.printFunc(user_id)>
<cfdocument  format="pdf" filename="contact_data.pdf" overwrite="Yes" name="contact_data.pdf"> 
    <cfinclude  template="master.cfm">
        <h3 class="text-center">Contact List</h3>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Date Of Birth</th>
                    <th>Gender</th>
                    <th>Address</th>
                    <th>Pincode</th>
                    <th>Email Id</th>
                    <th>Phone Number</th>
                </tr>
            </thead>
            <cfoutput query="data">
            <tbody>                
                <cfset c_name= title & ". " & first_name & " " & last_name>
                <cfset address_name= address & ", " & street_name & ", " & city & ", " & state & ", " & nation>
                <tr>                    
                    <td>#c_name#</td>
                    <td>#dob#</td>
                    <td>#gender#</td>
                    <td>#address_name#</td>
                    <td>#pincode#</td>
                    <td>#email_id#</td>
                    <td>#phone_number#</td>
                </tr>
            </tbody>
             </cfoutput>
        </table>
</cfdocument>
<cfprint type="pdf" source="#expandPath('.')#\contact_data.pdf" printer="OneNote (Desktop)">
    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    June 3, 2022

    What happens when you temporarily replace 

    <cfoutput><a href="print.cfm?user_id=#session.sessionUser.user_id#"><i class="fa fa-print"></i></a></cfoutput>

    with

    <cfoutput><a href="print.cfm?user_id=#session.sessionUser.user_id#"><i> Print it! </i></a></cfoutput>

    and then click on the link?

    BKBK
    Community Expert
    Community Expert
    June 5, 2022

    I now realize there is something else. You've given the same page the following 2 tasks:

    1. creating a PDF document and saving it to the file system;
    2. printing the PDF document.

     

    You want the first task to complete before the second one begins. Therefore, it is unwise to run both within the same page request, as you have done. 

     

    The solution is to treat the PDF creation and PDF printing as 2 separate, sequential tasks. Two common ways to do so are:

    1. Using 2 functions

    The first function, say createPDF(), uses cfdocument to create the document; the second function, say printDoc(), uses cfprint to print the document.

    2. Using named locks (which I would recommend in these circumstances)

     

    <!--- Locking ensures printing will start only after PDF generation has completed --->
    <cflock name="PDFGenerationPrintingLock" type="exclusive" timeout="5">
        <cfparam  name="user_id" default="v">
        <cfset doc_data=createObject("component","components.results")>
        <cfset data=doc_data.printFunc(user_id)>
        <cfdocument  format="pdf" filename="contact_data.pdf" overwrite="Yes" name="contact_data.pdf"> 
        ... etc. ...
        </cfdocument>
    </cflock>
    
    <cflock name="PDFGenerationPrintingLock" type="exclusive" timeout="10">
        <cfprint type="pdf" source="#expandPath('.')#\contact_data.pdf" printer="OneNote (Desktop)">
    </cflock>
    

     

    Yet another suggestion: Does the file print.log exist in your logs? If so, what does it say when you run the code?  

     

    Charlie Arehart
    Community Expert
    Community Expert
    June 5, 2022

    Bkbk, I believe this is not necessary: cfml does not process those two tags asynchronously. 

    /Charlie (troubleshooter, carehart. org)
    Charlie Arehart
    Community Expert
    Community Expert
    June 2, 2022

    First, what happens if you call your print.cfm page directly (passing in the needed id)? It doesn't look like it should produce any output (cfprint sends the doc to a printer). Does the printer show the doc being printed? And if so, does the print.cfm page produce any output on screen? If not, maybe just add a confirmation message after the cfprint (or put it in a try/catch in case it fails, to offer a different message).

     

    Second, what about your first template should be making "a print window" appear? I see tthing there to indicate that. 

    /Charlie (troubleshooter, carehart. org)
    abcd987Author
    New Participant
    June 3, 2022

    Can you help with a code, Does cfprint shows print window?

    Charlie Arehart
    Community Expert
    Community Expert
    June 3, 2022

    It does not, and that's what I was getting at. If you want to pop up a window, such as to tell the user their doc was printed (or not), that's indeed separate code. I can't offer it, as I don't do it.

     

    And while cf used to offer a cfwindow tag, that's been deprecated and such cf ui tags are discouraged in recent years. Instead, people should look to Javascript to handle that sort of activity.

     

    And perhaps something as simple as a js alert (perhaps styled in some way) may suffice, and that's as simple as:

    <script>
    alert('some msg');
    </script>

    But as for a real pop-up window (if you want more than that alert, once you've tried it), do beware that you may find only older references to such, as again it's generally less common to do that anymore. 

     

    Indeed, you may notice you don't see it often in sites you visit. But if there's some site that does just what you want, the good news is that will again likely be done via Javascript. And you'd be able to look at that in your browser, to see how it was done and then could implement that in your cfm template. 

     

    Hope that helps.

    /Charlie (troubleshooter, carehart. org)