I have a link that creates a pdf attachment.
create pdf attachment
It create a attached pdf and I would like to add a watermark image in the middle of the pdf?
filename.cfm file
<cfsetting enablecfoutputonly="true">
<cfcontent type="application/pdf">
<cfheader name="Content-Disposition" value="attachment;filename=nameoffile.pdf">
<cfdocument format="PDF" localurl="yes"
marginTop="0.1" marginLeft="0.2" marginRight="0.2" marginBottom="0.1"
pageType="custom" pageWidth="8.5" pageHeight="10.2">
...pdf content...
</cfdocument>
Have you looked at using the cfpdf tag? One of it's options is to add watermarks to PDF documents.
From the docs here:
Add a watermark to a PDF document
<cfpdf
required:
action = "addwatermark"
source = "absolute or relative pathname to a PDF file|PDF document variable|cfdocument variable"
one of the following:
copyfrom = "absolute or relative pathname to a PDF file from which the first page is used as a watermark"
image = "absolute or relative pathname to image file|image variable used as a watermark"
optional:
foreground = "yes|no"
isBase64 = "yes|no"
opacity = "watermark opacity"
overwrite = "yes|no"
pages = "page or pages to add the watermark"
password = "user or owner password for the PDF source file"
position = "position on the page where the watermark is placed"
rotation = "degree of rotation of the watermark"
showonprint = "yes|no">
Notice that the source attribute can be a cfdocument variable.
Related
I'm working on an app that creates customized images based on user inputs using canvas and I was wondering if it was possible to allow users to email themselves a copy of the final product in base64 or if I would have to convert it to .jpg or .png and then embed it as that.
Thanks.
I'd suggest converting the Base64 to an image, writing to disk, and using cfmailparam to attach/inline it as well as automatically remove it from disk.
I have had nothing but issues trying to directly inline/attach base64 images to emails using cfmail. I have had partial success converting the base64 to an image object (using ImageReadBase64()) and then using the image object as the value of the cfmailparam content attribute and ommitting the file attribute, however the image comes through with inverted colors oddly enough.
On to some example code...
<cfsavecontent variable="mailContent">
...
<img src="cid:signature">
...
</cfsavecontent>
<cfset sigImage = ImageReadBase64(SIGNATURE_IMAGE_BASE64)>
<cfimage source="#sigImage#" destination="tmpSigImage.png" action="write" overwrite="true">
<cfmail ...>
#mailContent#
<cfmailparam
file="#ExpandPath('./tmpSigImage.png')#"
contentid="signature"
disposition="inline"
remove="yes"
/>
</cfmail>
I'm creating a company directory using our existing Active Directory information. I'm able to pull all the data I need, but I wanted to use Active Directory for the photo as well.
I found this snippet of code on this blog: http://plus10.blogspot.com/2008/02/coldfusion-cfldap-display-images-stored.html
<!--- imageFile.cfm --->
<cfsilent>
<cfldap action="QUERY"
name="ldap"
attributes="jpegPhoto"
start="dc=[yourdc],dc=com"
filter="sAMAccountName=[loginname]"
server="[yourserver]"
username="[username]"
password="[password]">
<cfscript>
ldapPhoto = toString(ldap.jpegPhoto);
ldapPhoto = binaryDecode(ldapPhoto,"base64");
</cfscript>
</cfsilent><cfcontent type="image/jpeg" variable="#ldapPhoto#">
<!--- to display the image on a page --->
<img src="imageFile.cfm" width="100" height="125" alt="">
I plugged in all my server data and I get the error
The image "....imagefile.cfm" cannot be displayed because it contains errors
Why isn't the image displaying? and how can I correct the code?
I did a <cfdump> on the query and it just shows as "jpegPhoto" not binary data.
I can't post the actual page because it is on an internal network only.
Note, the poster answered her own question. The cfcontent and img tags should be replaced with a cfimage tag using the source attribute and action="writeToBrowser"
crediting the author: src: https://plus10.blogspot.com/2008/02/coldfusion-cfldap-display-images-stored.html
February 12, 2008
ColdFusion CFLDAP - Display images stored in Microsoft Active Directory
Not too hard once I found out how MS stores the data in the photoJpeg field of Active Directory.
<!--- imageFile.cfm --->
<cfsilent>
<cfldap action="QUERY"
name="ldap"
attributes="jpegPhoto"
start="dc=[yourdc],dc=com"
filter="sAMAccountName=[loginname]"
server="[yourserver]"
username="[username]"
password="[password]">
<cfscript>
ldapPhoto = toString(ldap.jpegPhoto);
ldapPhoto = binaryDecode(ldapPhoto,"base64");
</cfscript>
</cfsilent>
<cfcontent type="image/jpeg" variable="#ldapPhoto#">
<!--- to display the image on a page --->
<img src="imageFile.cfm" width="100" height="125" alt="">
I'm using CFScript to create and save a spreadsheet. However, SpreadsheetWrite isn't producing a file.
<cfscript>
...
...
...
spreadsheetWrite(sObj, yourSheet, "yes");
</cfscript>
It does not produce an error, but there's no file either. If I remove spreadsheetwrite from the cfscript tag and use:
<cfspreadsheet action = "write" fileName = "#yourSheet#" name = "sObj">
... I get the file just fine.
Was this not the intended use of SpreadsheetWrite()? Also, is it possible to produce a spreadsheet directly to the browser like cfdocument, or do I have to save the file first?
(Update: With relative paths it may be saving the file somewhere you are not expecting. Use an absolute path so there are no guessing games.)
This works for me using absolute paths.
<cfscript>
yourSheet = "c:/myFile.xls";
sObj = SpreadSheetNew("MySheet");
SpreadsheetAddRow(sObj,"Foo,Bar",1,1);
SpreadSheetWrite(sObj, yourSheet, true);
WriteOutput("Exists?= "& FileExists(yourSheet));
</cfscript>
To display the spreadsheet in the browser, use SpreadSheetReadBinary and cfcontent. Obviously use the appropriate mime type and file extension.
<cfheader name="Content-Disposition" value="attachment; filename=SomeFile.xls" />
<cfcontent type="application/vnd.msexcel" variable="#SpreadSheetReadBinary(yourSpreadSheetObject)#" />
I have a service on a Coldfusion 9 server that creates image banners on the fly for us. A separate machine has to save these files with something like:
wget http://myserver.com/services/local/bannerCreator/250x250-v3.cfm?prodID=3&percentSaving=19
The problem is that I can't think of how to get coldfusion to write out binary data without using a temporary file. At the minute the image is just displayed as an image tag like this:
<cfimage action = "writeToBrowser" source="#banner#" width="#banner.width#" height="#banner.height#" />
Any ideas? Or should I just use a temporary file?
I can't test because you're not giving any example code for how your images are generated, but have you tried something along this line?
<cfcontent reset="true" variable="#imageData#" type="image/jpg" />
Update: So I went ahead and created my own image; I'll assume you're doing something similar. This works perfectly for me:
<cfset img = imageNew("",200,200,"rgb","red") />
<cfcontent variable="#toBinary(toBase64(img))#" type="image/png" reset="true" />
This works without writing to file, and without using a virtual file system ("ramdisk")
If you have the binary bytes of an file/image, you can replace the output buffer with it's contents like so:
<cfscript>
// eg. this is how you get a file as a binary stream
// var fileBytes = fileReadBinary( '/path/to/your/file.jpg' );
// get the http response
var response = getPageContext().getFusionContext().getResponse();
// set the appropriate mime type
response.setHeader( 'Content-Type', 'image/jpg' );
// replace the output stream contents with the binary
response.getOutputStream().writeThrough( fileBytes );
// leave immediately to ensure no whitespace is added
abort;
</cfscript>
Pretty much what <cfcontent> does when you use reset="true"
The advantage of this method over <cfcontent> is that we can write it inside our cfscript based cfcs.
I found the solution above
<cfcontent variable="#toBinary(toBase64(img))#" type="image/png" reset="true" />
to not quite work for me.
Setting type="image/png" is just setting the mime type of the response. I don't think it's necessarily encoding the image as PNG. As such, generating a transparent png (image type "argb") was giving me odd colours, when compared to the <cfimage action = "writeToBrowser"...> method.
I figured that somehow I needed to explicitly encode the image data as PNG and output the binary data directly.
With some digging around in the underlying java, I came up with this, which so far seems to work for me.
This example draws a transparent png with a black circle.
<!--- create the image and draw it --->
<cfset img = ImageNew("", 23, 23, "argb")>
<cfset ImageSetDrawingColor(img, "black")>
<cfset ImageDrawOval(img, 0, 0, 21, 21, true)>
<!--- get the response object --->
<cfset response = getPageContext().getFusionContext().getResponse()>
<!--- set the response mime type --->
<cfset response.setHeader('Content-Type', 'image/png')>
<!--- get the underlying image data --->
<cfset bImage = ImageGetBufferedImage(img)>
<!--- get the magical object to do the png encoding --->
<cfset ImageIO = createObject("java", "javax.imageio.ImageIO")>
<!--- encode the image data as png and write it directly to the response stream --->
<cfset ImageIO.write(bImage, "png", response.getResponse().getOutputStream())>
I hope that helps someone!
Take out the height and width attributes and add the format attribute:
<cfimage action = "writeToBrowser" source="#banner#" format="png" />
wget should honor the redirection to the physical file CF creates in the CFFileServlet folder but if it doesn't there is a flag you can set to make it --max-redirect=10.
And as you suggest, a temporary file would work too. Just write the file and use cfheader and cfcontent to write it out. Just make sure to make the temp file name more unique.
<cfimage action="write" destination="tempfile.png" source="#banner#" format="png" />
<cfheader name="content-disposition" value="attachment; filename=banner.png" />
<cfcontent file="tempfile.png" deletefile="true" type="image/png" />
I do a similar thing, and you need to combine using tags and cfscript. There are image functions that are required. There are a lot of image functions available, once you have an image in memory as a variable. You can get the image into memory using CFFILE or CFHTTP or a number of other ways.
In my case, I read an image file into memory using CFIMAGE tag, then manipulate it with CFSCRIPT image functions by adding text to the bottom, then outputting the resulting image as a .png (or a .jpg if you prefer) to the browser. The only file stored on the server is the original image file. In your case instead of reading in an image you'd call it using a cfhttp tag as you already do. Here's my code:
<!---Get the image for processing ...--->
<cfimage action="read" source="#application.approotABS#\webcam\webcam.jpg" name="CamImage" />
<!--- prepare the text for overlay --->
<cfscript>
attr=structNew();
attr.font = "Arial";
attr.size = 15;
ImageSetDrawingColor(CamImage, "white");
ImageDrawText(CamImage, 'LIVE FROM STUDIO 1', 18,(ImageGetHeight(CamImage)-54), attr);
ImageDrawText(CamImage, '#ShowOnNow.showname#', 18,(ImageGetHeight(CamImage)-36), attr);
ImageDrawText(CamImage, datestring,18,(ImageGetHeight(CamImage)-18), attr);
</cfscript>
<!--- further down the page, output the manipulated image: ---->
<div class="webcam">
<cfimage action="writeToBrowser" source="#Camimage#" >
</div>
You can see it in action at http://hawkesburyradio.com.au/index.cfm?pid=111538
(I am using CF9 with Windows Server )
I am using <cfdocument> tag of coldfusion 7. Using CFEclipse and working on MacOS.
I have written the following code:
<cfdocument format="pdf">
SitePoint.com - Introduction to ColdFusion 7
PDF Generation
This is an example of PDF generation using ColdFusion 7.
</cfdocument>
But instead of asking me to save this file in .pdf format, its trying to open it in .cfm format.
How can I save it in .pdf format? Thanks!
Unless you tell it otherwise, the webserver returns the results of a CFM call as text. You need to use CFContent with CFHeader to alert the browser that the results it will be recieving are of a different type. Something like:
<cfheader name="Content-Disposition" value="inline; filename=document.pdf">
<cfcontent type="application/x-pdf">
<cfdocument>...</cfdocument>
I may have the MIME type wrong there. I'm doing this from memory. Check the docs for more help.
If you are on CF8 or higher then use the saveAsName attribute:
<cfdocument saveAsName=""
Either that or the method suggested by Ben above should work
You might also need to import the style sheet as well. So you can get the desired formatting. It needs to be imported after cfdocument.
<cfdocument
format="pdf"
filename = "canwriteurfile.pdf"
overwrite = "yes"
marginBottom = ".2"
marginLeft = ".4"
marginRight = ".4"
marginTop = ".2">
<style type="text/css">#import "pdf.css";</style>
BLAHHH BLAHHH PDF FORMATTING STUFF
</cfdocument>
Try:
<cfdocument format="PDF" name="myPDF">
<html>
<body>
SitePoint.com - Introduction to ColdFusion 7
PDF Generation
This is an example of PDF generation using ColdFusion 7.
</body>
</html>
</cfdocument>
<cfpdf action = "write" destination = "pdf_path" source = "myPDF" overwrite = "yes"/>
<cflocation url="pdf_path"/>
Whis this you save the PDF on disk