Download CFM page as Word Doc - coldfusion

I'm trying to render the output of a .cfm into a Word doc using cfinclude like so:
<cfheader name="content-disposition" value="attachment; filename=""MyDocument.doc""" />
<cfcontent type="application/msword" reset="true">
<cfinclude template="PageToDownload.cfm">
Since cfinclude outputs the .cfm as html, this should theoretically work. However, doing this downloads a document that errors in Word with "A text/xml declaration may occur only at the very beginning of input." I inspected the downloaded document with Notepad++ and saw this at the top:
<?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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
...
If I remove the empty line, the ?xml, and the !DOCTYPE (basically cut off everything up to <html), this document opens in Word. Is it possible to strip off the xml and DOCTYPE tags and start at <html using cfinclude? Or, is there another approach I should take?

Using your technique, what you can do is put the contents of PageToDownload.cfm into a variable using <cfsavecontent>. Once the contents are in said variable, you could strip out everything prior to the opening <html> tag and then <cfoutput> that variable after your <cfheader> and <cfcontent> statements.
Your code could look something like this.
<!--- Save contents of file to download into fileContents variable --->
<cfsavecontent variable="fileContent">
<cfinclude template="PageToDownload.cfm">
</cfsavecontent>
<!--- Strip out contents prior to the opening <html> tag --->
<cfset pos = findNoCase("<html", fileContent)>
<cfset fileContent = mid(fileContent, pos, 1000000)>
<!--- Output fileContent after the header statements --->
<cfheader name="content-disposition" value="attachment; filename=""MyDocument.doc""" />
<cfcontent type="application/msword" reset="true">
<cfoutput>#fileContent#</cfoutput>

Related

ColdFusion PDF "Open with" or "save file" prompt

How can I create a pop up that allows the user to "open with" or to save file as when dealing with coldfusion pdf's?
I am trying to figure out how to create it when this form is not actually mine and I am just using and prefilling a form that already exists.
<cfpdfform source="82040.pdf" action="populate">
<cfpdfformparam name="cust ##" value=""> <!---Section1 Customer Number--->
</cfpdfform>
I was able to create this pop up by doing something like this:
<cfsetting enablecfoutputonly="true">
<cfcontent type="application/pdf">
<cfheader name="Content-Disposition" value="attachment;filename=test.pdf">
<cfdocument format="PDF" localurl="yes"
marginTop=".25" marginLeft=".25" marginRight=".25" marginBottom=".25"
pageType="custom" pageWidth="8.5" pageHeight="10.2">
<cfoutput><?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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>PDF Export Example</title>
</head>
<body>
</body>
</html>
</cfoutput>
</cfdocument>
But am not sure how I can combine these two. Any help is greatly appreciated!
Per Request What I have tried:
<cfset tempFilePath = "c:/path/to/someUniqueFileName.pdf">
<cfpdfform source="82040.pdf" action="populate" destination="#tempFilePath#">
<cfpdfformparam name="org" value="Yes"> <!---Above Section1 Original ---> <!---On--->
</cfpdfform>
<cfheader name="Content-Disposition" value="attachment;filename=Testme.pdf">
<cfcontent type="application/pdf" file="#tempFilePath#" deleteFile="false">
By default, cfpdfform renders the result in the browser ie "inline". To return it as an attachment instead, save the content to a file or a variable. Then use cfheader/cfcontent to return it as an attachment.
File:
Use the "destination" attribute to save it to a file. To avoid conflicts with other threads be sure use unique file names. To automatically remove the temporary file when finished, set deleteFile="true"
<cfset tempFilePath = "c:/path/to/someUniqueFileName.pdf">
<cfpdfform source="82040.pdf" action="populate" destination="#tempFilePath#">
...
<cfheader name="Content-Disposition" value="attachment;filename=test.pdf">
<cfcontent type="application/pdf" file="#tempFilePath#" deleteFile="false">
Variable:
Use the "name" attribute to save the content to a variable. Though "name" is not listed in the documentation, this bug report suggests it is just a documentation error.
<cfpdfform source="82040.pdf" action="populate" name="pdfContent">
... etcetera ...
<cfheader name="Content-Disposition" value="attachment;filename=test.pdf">
<cfcontent type="application/pdf" variable="#toBinary(pdfContent)#">

Why is CFHTMLHEAD not working in ColdFusion 11?

I've tested several pages so far, and it seems that wherever I am attempting to use <CFHTMLHEAD> to add CSS or JavaScript to a CFM page in ColdFusion 11, it will not add it (works fine in CF8).
I read on SO about increasing the "Maximum Output Buffer size" in the Administrator > Server Setttings > Settings from the default valut of 1024KB, but every value I've tried (2048KB, 4096KB, and even the max allowed 999999KB) I get the same result - the content is not included in the <HEAD> tag when the page loads.
Has anyone else run into this and found a solution yet?
Code Sample:
htmlhead.cfm:
<cfif NOT ThisTag.HasEndTag>
<cfthrow message="Missing end tag for custom tag htmlhead." type="HeadWrap" />
</cfif>
<cfif ThisTag.ExecutionMode is "End">
<cfhtmlhead text="#ThisTag.GeneratedContent#" />
<cfset ThisTag.GeneratedContent = "" />
</cfif>
index.cfm:
<cfsilent>
<!---
Data Retrieval, validation, etc. here
--->
<cf_htmlhead>
<style type="text/css">
tr.status-RETIRED td {
color: #800000;
}
tr.status-PENDING td {
color: #008000;
}
</style>
<script type="text/javascript">
$(function(){
$(".options-menu").mouseleave(function(e){
$(this).hide("fast");
})
$(".options-menu-link").mouseover(function(){
$(".options-menu").hide("fast");
$(".options-menu[data-sku='" + $(this).data("sku") + "']").show("fast");
}).click(function(e){
e.preventDefault();
});
});
</script>
</cf_htmlhead>
</cfsilent>
<!---
Custom Tag layout.cfm contains the DOCTYPE declaration, html, head (loads jquery, jquery-ui, css, etc.), and body tags with a header and footer for each page.
--->
<cf_layout>
<!--- Page Content Here (a form, a table, some divs, etc.) --->
</cf_layout>
I've found that in CF11 anything added using a cfhtmlhead call that comes before a cfcontent reset="true" does not get added to the resulting document. Does the cf_layout tag you are calling after your cf_htmlhead contain a content reset?
For example,
<!--- This will not be added under CF11. --->
<cfhtmlhead text="<!-- I'm some content that will only be included in CF10 or lower. -->">
<cfcontent reset="true">
<!--- This will be added under CF11 and lower. --->
<cfhtmlhead text="<!-- I'm some content that will be added in CF11 and lower. -->">
... the rest of your view code ...
<!--- This is a sample of the content you will get under CF11 --->
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Head Testing</title>
<!-- I'm some content that will be added in CF11 and lower. -->
</head>
<body>
<h1>Page content</h1>
</body>
</html>

Cfdocumentitem pagebreak during loop gives extra blank page

I am using CF9, and ColdBox. What I am doing is looping through a query, and assigning a page break at the end using cfdocumentitem pagebreak. However, it always gives me an extra blank page at the end. It's pretty much driving me crazy, so I defer to expert advice.
<cfdocument format="PDF" overwrite="Yes" localUrl="yes" pageType = "letter">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<cfquery name="Receipts" dbtype="query">
SELECT distinct id_number
FROM rc.RC1
</cfquery>
<cfoutput>
<cfloop query="Receipts">
<!--- removed for brevity --->
<cfdocumentitem type="pagebreak" />
</cfloop>
</cfoutput>
</body>
</html>
</cfdocument>
If you don't want to display the pagebreak after the last item in the loop then you have to explicitly say that. If the current row of the query is not the last row in the query then display the page break.
<cfloop query="Receipts">
<cfif Receipts.currentRow NEQ Receipts.recordCount>
<cfdocumentitem type="pagebreak" />
</cfif>
</cfloop>

pagebreak on coldfusion cfcontent word document

I am using cfcontent for creating a Word Document.
Example :
<cfsavecontent variable="sStr">
part1
part2
part3
</cfsavecontent>
<cfheader name="content-disposition" value="attachment; filename=wordDoc.doc">
<cfcontent type="application/msword" variable="#sStr#">
I want page break after each part.
The output should be in landscape format. How can I do this?
For a page break you should be using:
<cfdocumentitem type="pagebreak" />
And landscape you can set by:
<cfdocument orientation = "portrait|landscape>
As far as I know it's not possible to change the orientation of an individual page.
For cfcontent of a file you've saved to create an MS Word document:
<br clear=3Dall style=3D'page-break-before:always;mso-break-type:section-break'>
Got Page orientation to work with ms word by using the following style and making sure in cfcontent reset="no";
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<title>Work Order Report</title>
<style>
#page Section1
{ size:11.0in 8.5in;
mso-page-orientation:landscape;
}
body.section1 {page:section1;}
</style>
<body class="section1">
<cfcontent type="application/msword" reset="no">

ColdFusion: Download Will Start Shortly

I was trying to run this code, and browser ask me to download pdf file which is good, But it was not loading remaining HTML code or you can say I could not see "Thank you Message".
Am I doing something wrong or missing something?
Need your help
<cfheader name="content-disposition" value="attachment; filename=134.pdf"/>
<cfcontent type="application/pdf" file="#ExpandPath( './134.pdf' )#"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Download Now</title>
</head>
Thank you for your interest in downloading
this file. Your download should begin shortly.
Maybe show the 'download will begin shortly' message first as a distinct page, which then redirects (JavaScript or meta tag...) to the .cfm that generates the PDF.
What you're trying to do is load the PDF file immediatly. As stated before you should have a basic HTML Page:
<html>
<head>
<meta http-equiv="refresh" content="2;url=getpdf.cfm?file=123.pdf">
..rest of head
</head>
<body>
Download will begin shortly.
</body>
</html>
This getpdf.cfm?file=123.pdf will be
<cfheader name="content-disposition" value="attachment; filename=134.pdf"/>
<cfcontent type="application/pdf" file="#ExpandPath( './134.pdf' )#"/>
The reason it wont work, is that you browser reads the "headers" and will basically see the file as a PDF file. Like you were downloading a ".pdf". Now you're putting one step in between and let the browser redirect to the PDF download.