ColdFusion: force browser to download file and still load the page - coldfusion

I have an test.cfm where I dynamically build a pdf-File and output it with <cfheader> and <cfcontent> to the browser, but I still want the page to load and show "test html":
<CFFILE action="readbinary" file="#expandpath("./test.cfm")#" variable="testcontent" />
<CFHEADER name="Content-Disposition" value="attachment; filename=""test.txt""; charset=utf-8">
<CFCONTENT type="text/plain" reset="yes" variable="#testcontent#">
<CFCONTENT type="text/html" reset="yes" /><!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Testdoc</title>
</head>
<body>test html</body>
</html>
(outputting the file itself is just for the example)
Is there a way to accomplish this?

You can't trigger what happens after a download, but you can do it the other way round - load the page you want, then redirect to the file to download using a HTML meta redirect:
<meta http-equiv="refresh" content="5; url=http://domain.com/path/to/download" />
(If necessary, you can use cfhtmlhead to insert that into the relevant part of an existing HTML page.)
The 5 is the number of seconds to wait - setting to 0 will redirect immediately. (When using with other pages, using an instant redirect can cause issues with the back button; should be less of an issue for downloads though.)

Related

Execute ColdFusion Tags

I am at learning stage in ColdFusion. Now I am trying to execute below code to display variable content by using <cfset> and <cfoutput> tags. I completed my server installation and I am able to login to server with admin credentials. But it executed as normal html page, not getting the result from ColdFusion tags.
Do I need to install anything apart from this?
<!DOCTYPE html>
<html>
body>
<body background="download.jpg">
<cfset name ="swanav"/>
<cfoutput>#name#</cfoutput>
</body>
</html>
A small correction to your code,
<!DOCTYPE html>
<html>
<body background="download.jpg">
<cfset name="swanav" />
<cfoutput>#name#</cfoutput>
</body>
</html>
The code needs to be placed in,
/cfusion/wwwroot

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>

HTML Page to PDF Issues

The project I have is to create a Vista Print like application. I have created a very basic interface that places a resizable div in an area that can be dragged around. I am also able to place text into this div via pop-up iframe with designmode. I then write this to a database.
I then call this data and create a pdf using Coldfusions cfdocument.
The issue is when the PDF is created the font looks slightly thicker and the word wrap is different than the html page interface which is very important. Note: The word wraps are determined by the divs height and width. It looks fine on the cfdocument process page it is just off when the pdf is created. Anyone have any ideas on this or can point me to some info that could help?
Thanks in advance.
Code Here:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<cfset webRes = "96">
<cfset pxlWidth = "921">
<cfset pxlHeight = "1178">
<cfset inWidth = (pxlWidth/webres)>
<cfset inHeight = (pxlHeight/webres)>
<cfquery name="qTextData" datasource="ds">
SELECT DocID, TextID, Width, Height, PosX, PosY, FontFamily, FontColor, FontSize, TextValue
FROM TextEditor
WHERE ((DocID = #session.docid#))
</cfquery>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create PDF</title>
</head>
<cfoutput>
<cfdocument filename="mypdf.pdf" name="mypdf" fontembed="yes" format="pdf" overwrite="yes" pageheight="#inHeight#" pagewidth="#inWidth#" pagetype="custom">
<cfdocumentsection margintop="0" marginright="0" marginbottom="0" marginleft="0">
<body align="center" marginheight="0" marginwidth="0">
<div id="pageContainer" style="background-image:url(resources/image.caspx.jpg); background-repeat:no-repeat; height:#pxlHeight#px; width:#pxlWidth#px;">
<cfloop query="qTextData">
<div id="divTextField_#TextID#" style="position:absolute; top:#PosY#px; left:#PosX#px; width:#width#px; height:#Height#px; color:#FontColor#; font-family:#FontFamily#; font-size:#FontSize#pt; vertical-align:text-top; line-spacing:normal;">
#TextValue#
</div>
</cfloop>
</div>
</body>
</cfdocumentsection>
</cfdocument>
</cfoutput>
<script type="text/javascript">
window.open('http://linktopdf/mypdf.pdf','newWin','resizable=1');
</script>
</html>
I tried making this a comment but I started getting long winded and it was too large so it may not be the "answer" but if nothing else I'm sure it will have some helpful information in it.
What % are you viewing at and have you tried printing it out?
I've noticed that things can look lopsided or bigger than usual because they are just rendered weird in acrobat (or your PDF viewer of choice). Try adjusting the zoom and see if the display changes, also see if it prints correctly.
I have also found that like James suggested, I need special css just for the document. The CSS could be as odd as making one side of the div have a 1px larger border to make it look correct. Also keep in mind that although you view PDFs on the screen it is still a "print" media. PT means 1/72 of an inch which will look bigger when you scale from your 19+ inch monitor (which is further effected by your pixel resolution) to an 8.5" x 11" piece of paper that doesn't truly know what a pixel is. You may need to try adjusting your font size on the fly, something like: font-size: #fontSize-2#pt;. Change -2 to anything that may work for your output. You could also try using a unit that scales, like em.
The cfdocument docs list all the CSS that is supported as well as identify some restrictions. It does not appear that line-spacing:normal; is supported. Also make sure all your CSS is included inline or in a style tag but avoid using a linked .css file. I see you're doing that, just adding tips and pointers.
Also, according to the docs:
ColdFusion does not return HTML and CFML outside of
the <cfdocument></cfdocument> pair.
That means ColdFusion is processing your cfset statements but it does not return
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create PDF</title>
</head>
and
<script type="text/javascript">
window.open('http://linktopdf/mypdf.pdf','newWin','resizable=1');
</script>
</html>
So it is possible that iText isn't rendering it as you would expect because
It does not have a doctype
It is an ill-formed HTML document

vBulletin Facebook LIKEs thumbnail

I'm running vBulletin 4.1.5 and I have configured it's Facebook settings. The problem I'm facing is when someone clicked LIKE button, the thumbnail posted in Facebook is not the first image in the thread, it will pick randomly from anywhere in the page i.e. avatar of any user replied in that thread!
Appreciate your help!
one solution is that you can define a global "facebook-thumbnail" for all shares.
Go to
AdminCP
Settings
Options
Facebook Options
Image URL
This image should be your site-logo or something else.
I'm still programming with vB3.8.x, however I'm working on a plugin for facebook liking. Might help you write you're own for vB4.x
First, you need opengraph and facebook namespaces defined:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml">
<head profile="http://gmpg.org/xfn/11">
Second, you need facebook's javascript:
<scr ipt type="text/javascript" src="http://connect.facebook.net/en_US/all.js#xfbml=1">
</scr ipt>
Last, you need open graph tags defined in the section of the html page:
<meta property="og:type" content="article" />
<meta property="og:title" content="this shows up as article's title when you like" />
<link rel="image_src" href="http://www.yoursite.com/yoursite1.jpg" />
<meta property="og:image" content="http://www.yoursite.com/yoursite1.jpg" />
<link rel="image_src" href="http://www.yoursite.com/yoursite2.jpg" />
<meta property="og:image" content="http://www.yoursite.com/yoursite2.jpg" />
<link rel="image_src" href="http://www.yoursite.com/yoursite3.jpg" />
<meta property="og:image" content="http://www.yoursite.com/yoursite3.jpg" />
<link rel="image_src" href="http://www.yoursite.com/yoursite4.jpg" />
<meta property="og:image" content="http://www.yoursite.com/yoursite4.jpg" />
The plugin I'm working on will loop through the top 5 attachments for a post and write out image_src tags and og:image tags. Not yet sure which hook I'll use.
pseudo code:
$attachs = $db->query_read_slave("
SELECT attachmentid, attachment.dateline
FROM " . TABLE_PREFIX . "post AS post
INNER JOIN " . TABLE_PREFIX . "attachment AS attachment ON (attachment.postid=post.postid AND attachment.visible=1)
WHERE threadid = $threadinfo[threadid]
AND post.visible = 1
ORDER BY filename DESC
LIMIT 5
");
if ($db->num_rows($attachs))
{
while ($attachment = $db->fetch_array($attachs))
{
$strImages = "<meta property=\"og:image\" content=\"/attachment.php?attachmentid=$attachment[attachmentid]&d=$attachment[dateline]\" />"
}
}
FYI - facebook seems to add thumbnails in a reverse manner. They'll look at the last og:image and use that as the first thumbnail; the next thumbnail shown would be the second to last and so on. Might want to play with the sort order of the SQL if that's important. Also, the image_src link is used for non-open graph social sites (google plus, twitter)

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.