Sunday, January 13, 2013

ColdFusion - cfdocument, pdf and html tables

cfdocument tag of ColdFusion 8 is a great tool to generate extremely good looking PDF files/reports. Yesterday, when I was working on a PDF Generation System built on ColdFusion, I just noticed that cfdocument do not understand style="border-collapse: collapse" style. Therefore, if you are using tables in your PDF, you will see a lots of spaces and thick borders all around your tables and cells.

I highly recommend to use tableless structure inside cfdocument to avoid such problems. But if is absoulte necessary for you to use tables, then here is a fix for you :-)

  • Make sure that values of attribute border, cellpadding and cellspacing is set to 0 in each table. e.g <table cellspacing="0" border="0" cellspacing="0">
  • Add following style in your style sheet.
       td {
    margin-left:0;
    border-bottom:0;
    border-top:0;
    border-right:0;
    padding-left:0;
    }
Please also keep in mind that do not use <hr/> tag to generated colored horizontal line. Instead use colored image. And last but not the least, if your code inside cfdocument tag is XHTML validated, you will get perfect output.

Saturday, January 12, 2013

Downloading attachments with cfimap

<cfimap> is very easy to use but it can become a real pain if you are using ColdFusion at shared hosting, specially when downloading attachments. <cfimap> by default save attachments in temp folder (GetTempDirectory()) but on shared hosting, your host may not give you read/write access to that folder. In such situation, you can use "attachmentpath" attribute of cfimap to define path where cfimap should download the attachments. Unfortunately, this may also not work on shared hosting. Here is a workaround which I use to download attachments.

<!--- Creating a Temp Attachmetn Folder in Memory ---> 
        <cfset sfolder = "ram://attachments" />
        <cfif NOT DirectoryExists(#sfolder#) >
            <cfdirectory
                action="create"
                directory="#sfolder#"
            />       
        </cfif>
    <cfimap
        server = "IMAP SERVER"
        username = "MyUserName"        
        action = "open"
        secure="yes"
        password = "MyPassword"
        connection = "imapEmail"
        timeout="300" generateUniqueFilenames="Yes">   
   
    <cfimap action="GetAll" AttachmentPath ="#sfolder#" connection="imapEmail" name="AllEmails">    


    <cfoutput>
        <!--- Looping directory files --->
            <cfdirectory
                name="files"
                action="list"
                directory="#sfolder#"
            />
            <cfloop query="files">
                <cffile action="copy"
                      destination="C:\inetpub\wwwroot\mydomain.com\wwwroot\attachments"
                      nameconflict="MakeUnique"
                      source="#sfolder#/#files.name#">
                 #files.name# has been downloaded. <br/>    
                <cffile action="delete" file="#sfolder#/#files.name#" />
            </cfloop>
    </cfoutput>

    <cfimap action="close" connection = "imapEmail">

First, we create a folder "attachments" in Memory and then downloaded all attachment in that folder. Later, we loop through all the files in "attachments" directory and copy them to our destination folder. Simple!!

Wednesday, January 9, 2013

Invalid public shared ip address in platform server configuration for shared IP address usage

Yesterday, I was installing WebSitePanel (WSP) 2.x for a client. After successful installation, when I tried to create a website, WSP showed an error message and failed to create website. Error was "Invalid public shared ip address in platform server configuration for shared IP address usage". 


After a little search, I found this article:
http://www.websitepanel.net/making-the-required-changes-to-your-iis-service-global-dns-records-to-support-websitepanel-2-0/

However, when I checked settings of WSP, Public Shared IP addressed was already there.


Drilling the source code of WSP revealed that WebServerController.cs looks for a Service Property "PublicSharedIP". Looking at the ServiceProperties table in WSP database, I found that this properly "PublicSharedIP" does not exists!!

So, I executed following query to add "PublicSharedIP" property into database:
INSERT INTO ServiceProperties (ServiceID, PropertyName, PropertyValue) VALUES(2, ‘PublicSharedIP’, 1)

(1 is my Public IP, listed in IPAddresses table)

Problem Fixed!!