<cfpdf

Hi
Im wodering can you have a <Cfpdf Tag inside a <cfmail tag?

Im trying to add an Image to an Invoice but its keeping the same image for all invoices, but yet when I dump out the values of the query its showing the correct image for each invoice

so I thaught mabe you cant do this inside a <cfmail tag

If so, I dont know what the problem can be

Since you didn’t post any code, it’s hard to make suggestions. But what makes you think the problem is cfmail? Does your code produce the desired results without it?

Please don’t take this the wrong way, but from some of your other posts, I think you could really benefit from thinking about how to best approach and investigate your code issues. In the long run it will save you a lot of time and headaches, and make you a better developer IMO.

For example, if you’re having a problem getting the same image on a cfpdf, start with the basics. Can you produce the correct pdf’s with simple code before throwing a bunch of extra layers into the equation (like cfmail). Once extra variables enter the mix, they can obfuscate the problem. Reduce things down to the basics first. Get the code to work one piece at a time. Then add extra layers. Again, one piece at a time. Then if something doesn’t work … you’ll know exactly where the problem is …

Good luck

don’t know that you SHOULD do that inside a cfmail tag. Your trying to create something that might take a little bit (in computer time) and the pdf might not be created when it’s trying to send the email.

Create the pdf THEN send the email and the delete the pdf. Seems the most logical order to me.

Probably not. But I was hoping my comments might encourage them to start thinking about the whole process in a different light :wink:

Thanks for your advice cfstarlight, I tried the basics, its sending the emails fine, to the right customers also, its just not changing the image for each company im in. even though I dump out the values of the templates it giving me the correct ones. but then when I press submit to send all emails it goes back to putting the same image on all PDF’s

I even tried hard coding the image into here:


image = "#request.mainfilePath#templates\\#image = "#request.mainfilePath#templates\\#qgetcreditnotetemplate.InvoiceTemplate_FileName#".InvoiceTemplate_FileName#"

and it works fine it uses that image, but when I link back up to my Query it gives one image for all if that makes sence

and the query “qgetcreditnotetemplate” is correct as I said above when I dump out the values if it, Its outputting the correct Images

Everything works right up until the image is added to the PDF

I think I might have to do what downtroden has suggested and Create the pdf then send the email.


<!--- Count the Emails as they are sent --->
<cfquery name="qgetemailcount" datasource="#request.dsn#">
					select INVOICEEMAILLOG_Key
					from INVOICEEMAILLOG IE
					where INVOICE_Key = INVOICE_key
					AND INVOICEEMAILLOG_Type = 1
					</cfquery>	

<cfmail query="Getemails"
   from="#Company_Email#"
   to="#Customer_Email#"
   subject="#COMPANY_Name#     Invoice Number: #INVOICE_Number#"
   replyto="#Company_Email#">

<cfquery name="qaddintolog" datasource="#request.dsn#">
insert into INVOICEEMAILLOG
(INVOICEEMAILLOG_To,INVOICEEMAILLOG_Replyto,INVOICE_Key,INVOICEEMAILLOG_Type)
values
<!--- Set InvoiceEmailLog to 1 for emailing --->
	<cfset INVOICEEMAILLOG_Type = 1>

('#Customer_Email#','#Company_Email#','#INVOICE_Key#','#INVOICEEMAILLOG_Type#')
</cfquery>
Dear #Customer_DelvName#
Please see attachment below
<cfquery name="qgetinvoice" datasource="#request.dsn#">
	select INVOICE_PDFFile
	from INVOICE I, COMPANY C, CUSTOMER CU, InvoiceTemplate IT
	where I.Company_Number = C.COMPANY_Number
	AND I.CUSTOMER_AccNum = CU.CUSTOMER_AccNum
	AND IT.Company_Number = I.Company_Number
	AND IT.Company_Number = I.Company_Number
	AND IT.InvoiceTemplateType_Key = I.InvoiceTemplateType_Key
	AND CU.CUSTOMER_DelvNum = I.CUSTOMER_DelvNum
	AND I.INVOICE_Number = #INVOICE_Number#
	AND CU.Customer_Email <> ''
</cfquery>

   


<cfset timestamp = timeformat(now(),"HHmmss")>
<cfset pdfname = INVOICE_Key&"_"&timestamp&".pdf">
<cfpdf action="getinfo"  name="pdfinfo" source="#request.mainfilePath#PDFs\\#qgetinvoice.INVOICE_PDFFile#">

<cfif pdfinfo.TotalPages gt 1>
	<cfset totalpages = "1-"&pdfinfo.TotalPages>
	
<cfelse>
	<cfset totalpages = 1>
</cfif>
 <!--- Get the template for the Invoices --->
<cfquery name="qgetcreditnotetemplate" datasource="#request.dsn#">
	select InvoiceTemplate_FileName
	from InvoiceTemplate IT, INVOICE I, COMPANY C, CUSTOMER CU
	where I.INVOICE_Key = I.INVOICE_Key
	AND I.Company_Number = C.COMPANY_Number
	<cfif isdefined ('form.DateFrom')>
	AND I.COMPANY_Number = #form.COMPANY_Number#
	AND IT.InvoiceTemplateType_Key = I.InvoiceTemplateType_Key
	AND IT.Company_Number = I.Company_Number
	AND I.CUSTOMER_AccNum = CU.CUSTOMER_AccNum
	AND I.CUSTOMER_DelvNum = CU.CUSTOMER_DelvNum
	AND CU.Customer_Email <> ''
	<cfif isdefined ('form.DateFrom')>
	<cfif form.DateFrom neq ''>		
	</cfif>
	</cfif>
	</cfif>
</cfquery> 

 <cfpdf
    action = "addwatermark"
    source = "#request.mainfilePath#PDFs\\#qgetinvoice.INVOICE_PDFFile#"
    image = "#request.mainfilePath#templates\\#qgetcreditnotetemplate.InvoiceTemplate_FileName#"
    foreground = "No"
    overwrite = "yes"
    pages = "#totalpages#"
	opacity = "10"
	Position = "10, -50"
    showonprint = "YES"
    destination = "#request.maindrivePath#temppdf\\#pdfname#">
	<cfmailparam file="#request.maindrivePath#temppdf\\#Invoice_Key&"_"&timestamp&".pdf"#" type="application/JPG">
	
Regards,
#Company_Name#
#Company_Address1#, #Company_Address2#
#Company_Address3#, #Company_Address4#
#Company_Phone#, #Company_Fax#
</cfmail>


couple of questions
this:


<!--- Count the Emails as they are sent --->
<cfquery name="qgetemailcount" datasource="#request.dsn#">
	select INVOICEEMAILLOG_Key
	from INVOICEEMAILLOG IE
	where INVOICE_Key = INVOICE_key
	AND INVOICEEMAILLOG_Type = 1
</cfquery>	

this may be ignorance of Ms SQL on my part (if that’s what you’re using) but why use SQL to count emails sent? Why not use a variable in your loop that counts as it sends?

The “qaddintolog” query, setting variables inside of queries is bad practice (at least in my opinion).

From what I can tell, “qgetinvoice” and “qgetcreditnotetemplate” could probably be combined into one SQL call. You might look at that. I could be wrong.

One more thing, I tried looking over the page, but it’s a little hard to read. Pick camel case or underscoring for naming variables. In the long run (for you AND others) it’s easier to read and don’t be afraid to comment more. I’ve learned the hard way that’s it’s not just for other people but for YOU when you have to come back to something. Bottom line, if I were you I would be looking to refactor my code.

Stuff like “qgetcreditnotetemplate” query. There’s an if statement:


<cfif isdefined ('form.DateFrom')>
     <cfif form.DateFrom neq ''></cfif>
</cfif>

that does absolutely nothing, but the machine still has to contemplate it which is a waste of resources.

Hi downtroden
You could be right about counting using a variable rather than MYSQL, but I found it easier to do it that way and it worked so I was happy enough :slight_smile:

I changed the query “qgetcreditnotetemplate” to the below now which I finally got picking the correct template for each Invoice, the only problem now is it will only send one at a time, if I check two or more invoices I get a error message

Error Executing Database Query.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘309 AND CU.Customer_Email <> ‘’ AND I.CUSTOMER_DelvNum = CU.CUSTOMER’ at line 4

I think I need to loop it around the <Cfpdf tag, but where ever I put it, it does’nt seem to work for me

any tips?


<cfif isdefined ('form.companynumber')>
<cfquery name="qgetcreditnotetemplate" datasource="#request.dsn#">
	SELECT	InvoiceTemplate_FileName
	FROM	        InvoiceTemplate IT, INVOICE I, COMPANY C, CUSTOMER CU
	WHERE	IT.InvoiceTemplateType_Key = I.InvoiceTemplateType_Key
        AND		I.Invoice_Key = #form.INVOICE_Checktosendemail#
	AND          CU.Customer_Email <> ''
	AND          I.CUSTOMER_DelvNum = CU.CUSTOMER_DelvNum
	AND          IT.Company_Number = I.Company_Number
</cfquery>
</cfif>

Nevermind got it working I did what you said

From what I can tell, “qgetinvoice” and “qgetcreditnotetemplate” could probably be combined into one SQL call. You might look at that. I could be wrong.

I combined both queries together and its working perfect now, I think :slight_smile:

Thanks