Skip to main content
Inspiring
March 27, 2019
Answered

CFLOOP LIST & CFDOCUMENT

  • March 27, 2019
  • 2 replies
  • 2693 views

I have code to create a pdf like this. It works great

<cfdocument format="pdf" localUrl="yes" orientation="landscape" marginleft="0.10" marginright=".10" margintop="0.15" >

<cfdocumentitem type="footer"> <cfoutput>Page #cfdocument.currentpagenumber# of #cfdocument.totalpagecount#</cfoutput> </cfdocumentitem>

<cfquery name="Loads" datasource="dsn">

SELECT  [company_id]

FROM datatable

WHERE ID  =16598

</cfquery>

Rest of code based on ID

</cfdocument>

Now I want to loop through a list of ID's and create a combined pdf of all of the ID's in the list

like this

<cfloop list="16958,17019,17020"  index="idx">

<cfdocument format="pdf" localUrl="yes" orientation="landscape" marginleft="0.10" marginright=".10" margintop="0.15" >

<cfdocumentitem type="footer"> <cfoutput>Page #cfdocument.currentpagenumber# of #cfdocument.totalpagecount#</cfoutput> </cfdocumentitem>

<cfquery name="Loads" datasource="dsn">

SELECT  [company_id]

FROM datatable

WHERE ID  =#idx#

</cfquery>

Rest of code based on ID

</cfdocument>

</cfloop>

But when I do this only the first pdf prints.

So in my example, the pdf for 16958 is generated but not 17019 or 17020

What am I doing wrong?

This topic has been closed for replies.
Correct answer EddieLotter

If you want a single document, move the loop inside of the cfdocument tag.

2 replies

EddieLotter
EddieLotterCorrect answer
Inspiring
March 28, 2019

If you want a single document, move the loop inside of the cfdocument tag.

BKBK
Community Expert
Community Expert
March 27, 2019

It's going to be one PDF at a time. If you want to generate more, then do something like

<cfloop list="16958,17019,17020"  index="idx">

    <cfquery name="Loads" datasource="dsn">

    SELECT  [company_id]

    FROM datatable

    WHERE ID  =#idx#

    </cfquery>

    <cfdocument format="pdf" localUrl="yes" orientation="landscape" marginleft="0.10" marginright=".10" margintop="0.15" filename="#expandPath('doc#idx#.pdf')#" overwrite="yes">

    <cfdocumentitem type="footer"> <cfoutput>Page #cfdocument.currentpagenumber# of #cfdocument.totalpagecount#  </cfoutput> </cfdocumentitem>

    Rest of code based on ID

    </cfdocument>

</cfloop>

That will save each PDF as a separate file within the current directory.

weezerboyAuthor
Inspiring
March 28, 2019

This is exactly what I have now and it only generates 1 pdf form the list

EddieLotter
Inspiring
March 28, 2019

weezerboy  wrote

This is exactly what I have now and it only generates 1 pdf form the list

Not according to the code your provided. You have the cfdocument tag inside the cfloop tag. You need it to be the other way around.