Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Generating Word Document with alignment

Community Beginner ,
Feb 27, 2022 Feb 27, 2022

Hi,

 

I am using ColdFusion 2018. I am printing a word document (dynamic) using the below code. But there are multiple issues. One is that the image (please refer screenshot) is not aligned.  I want the image to be placed on top on extreme left and top (topmargin=0 and leftmargin=0).  Also after the image i am placing a text which is not displayed in the generated word document.  Please help.  If there is a better way to generate word document in coldfusion please post that too.  

 

<html>
<head><style> body { margin:0; } </style></head>
<body>
<img src="https://www.getinge-training.com/images/TopHeader.jpg" width="1429" height="496" border="0">
</body>
<cfheader name="content-disposition" VALUE="attachment; filename=1.doc">
<cfoutput>
<table border="0" cellpadding="0" cellspacing="0">
<cfset type="application/msword">
<tr>
<td><img src="https://www.getinge-training.com/images/TopHeader.jpg" width="1429" height="496" border="0"></td>
</tr>
<tr>
<td>PERSON NAME</td>
</tr>
</table>
</cfoutput>
</body>
</html>

2.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 03, 2022 Mar 03, 2022

@Sandhya231275905kpl , l shall say it again: all of these attempts (with an HTML table, cfheader with Word Content-Disposition, styles, etc.) will NOT produce the display you want. It is well-nigh impossible for the margins that you set in HTML to carry over to MS Word.

 

Imagine you wish to frame a picture. Obviously, the dimensions and aspect-ratio of the frame and mat board will be the factors that determine how well your picture will fit.

BKBK_0-1646342945696.png

You may be able to adapt the picture's dimensions. B

...
Translate
Community Expert ,
Feb 27, 2022 Feb 27, 2022

ColdFusion is irrelevant to your question, as your script contains no ColdFusion code. Ignoring cfheader, your script is basically HTML. So you should sort out the alignment in HTML first, preferably before any ColdFusion intervention.

 

For a start, the HTML is confusing. Reasons:

  1. One <body> tag, but two </body> tags;
  2.  It is unclear whether the table is within or outside the HTML body. 
  3. The same <img> tag occurs twice for no apparent reason;
  4.  Use of the cfoutput tag is unnecessary.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 27, 2022 Feb 27, 2022

Hello,

 

I am pasting the updated code below.  As you mentioned that ColdFusion is irrelevent, same way those errors are irrelevent as it will not make any difference to the code.  I hope you understood the solution that i am looking for (thanks for spending some time to find some mistakes in my dirty code through).  I hope anybody who look at the screenshot will know what i am looking for.   To make you happy I added some coldfusion code even then it is the same result.  Please help only if you understood the requirement, else do not comment so that it will leave it open for others to give some suggestions that i am looking for. 

 

<CFQUERY NAME="getreg" datasource="#client.datasource#">
SELECT fname,lname FROM reg WHERE reg_id = 39027
</CFQUERY>
<html>
<head>
<style> body { margin:0; } </style>
</head>
<body topmargin=0 leftmargin=0>
<cfheader name="content-disposition" VALUE="attachment; filename=1.doc">
<cfoutput>
<table border="0" cellpadding="0" cellspacing="0">
<cfset type="application/msword">
<tr>
<td><img src="https://www.getinge-training.com/images/TopHeader.jpg" width="1429" height="496" border="0"></td>
</tr>
<tr>
<td>#getreg.fname#</td>
</tr>
</table>
</cfoutput>
</body>
</html>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 27, 2022 Feb 27, 2022

As you mentioned that ColdFusion is irrelevent, same way those errors are irrelevent as it will not make any difference to the code.  


By @Sandhya231275905kpl

 

I would disagree. HTML is tolerant and flexible. If you make a mistake in HTML, browsers will likely ignore them and display the page in the best way they can.

 

I tested your script as follows:

1) I created a 1429 px by 496 px copy of your image and placed it in the same directory as the test CFM page.

2) I created  test.cfm, which contains:

<html>
<head>
<style> body { margin:0; } </style>
</head>
<body topmargin=0 leftmargin=0>
<cfheader name="content-disposition" VALUE="attachment; filename=1.doc">
<cfoutput>
<table border="0" cellpadding="0" cellspacing="0">
<cfset type="application/msword">
<tr>
<td><img src="http://localhost:8500/workspace/CF_Project/TopHeader.jpg" width="1429" height="496" border="0"></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
</table>
</cfoutput>
</body>
</html>

 

3) I ran test.cfm. The result is a Word page in which the image is located in the top-left corner. (image of result attached) 

BKBK_0-1646002374246.png

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 27, 2022 Feb 27, 2022

Tried, getting the same result.  Look like your output is in web layout.  See if you get the proper output by seeing the word content in "Print Preview" with ruler enabled.   I think I will have to work on something more to fix this as i don't see any difference from my code.  

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 28, 2022 Feb 28, 2022

I have attached the print-preview. Is it what you expect?

BKBK_0-1646039940402.png

 

Have you considered my suggestion? Reminder:

1. Create the HTML page, using the necessary styles to position the image and page elements.

2. Get the HTML page to display precisely as you want.

3. Introduce CFML code into the mix only after 2.

 

I am not preaching. I myself have been using this method for years.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 28, 2022 Feb 28, 2022

There is whitespace in the print preview, this is not what i am looking for.  I am expecting the output like this (attached) where there is no whitespace on left, right, top.  Bottom i will fill in with some more content using coldfusion cfquery.  In the preview you can see that the image is filled in the page width completely where there is no whitespace left off.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 28, 2022 Feb 28, 2022

How did you create this version that has your desired perfect alignment? With Word, not involving html? As bkbk noted, this doesn't seem a cf issue, unless you are saying you created an html page which "works perfectly", but then changes when served via cf, without any change (and even without cfml) 

 

Fwiw, we discussed in another thread last week the same image/page formatting, which we helped you solve as a pdf. Now you're moving on to a word document. As you can see, we do try to help.

 

I suspect you just wish we'd offer the answer rather than engage in "20 questions" and troubleshooting. If we had it, we would. And if you wait long enough, others may.

 

If you feel we've hit a roadblock and you need in our from more people, there are still other places you can ask for CF help, which I list in a category of my cf411.com site, cf411.com/cfhelp.


/Charlie (troubleshooter, carehart. org)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 28, 2022 Feb 28, 2022

Thank you is all i can reply.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 02, 2022 Mar 02, 2022

I managed to print the content in word using the below code.  Still there is whitespace on top and left which i wanted to avod (please see screenshot).  Please reply if anyone can help.  

 

<CFQUERY NAME="reg" datasource="#client.datasource#">
SELECT first_name,last_name,credential FROM registration
WHERE registration_id = #url.id#
</CFQUERY>
<CFQUERY NAME="req" datasource="#client.datasource#">
select city,state from evh_request where event_id=#url.eventid#
</CFQUERY>
<html>
<head>
<title>Certificate</title>
<style>
body { margin:0; margin-left: 0px;margin-top: 0px;}
.normaltxt { font-family: Arial, Helvetica, Sans-serif;font-size: 25px; border: none;FONT-WEIGHT: normal; }
.namebold { font-family: Arial Black, Helvetica, Sans-serif;font-size: 52px; border: none;FONT-WEIGHT: bold;}
.boldtxt { font-family: Arial Black, Helvetica, Sans-serif;font-size: 28px; border: none;FONT-WEIGHT: bold; }
</style>
</head>
<body>
<cfheader name="content-disposition" VALUE="attachment; filename=1.doc">
<cfset type="application/msword">
<table border=0 cellspacing=0 cellpadding=0 width=200>
<tr><td><img src="https://www.getinge-training.com/images/TopHeader.jpg" width="630" height="220" border="0"></td></tr>
<tr><td></td></tr><tr>
<td align=center height=140 valign=middle><font class="normaltxt">This certificate is to confirm</font>
</td></tr>
<tr><td align=center height=180 valign=middle><font class="namebold"><cfoutput>#reg.first_name# #reg.last_name# #reg.credential#</cfoutput></font></td></tr>
<tr><td align=center height=120 valign=top><font class="normaltxt">attended a didactic presentation and<br>received cadaver laboratory training on<br>Endoscopic Vessel Harvesting utilizing the<br><br></font></td></tr>
<tr><td align=center height=130 valign=middle><font class="boldtxt">Vasoview&#174; Endoscopic Vessel Harvesting System<font></td></tr>
<tr><td align=center height=150 valign=middle><font class="normaltxt"><cfoutput>#DateFormat(Now(),"mmmm d, yyyy")#</cfoutput><br><cfoutput>#req.City#, #req.state#</cfoutput><br></td></tr>
<tr> <td align=left height=60 valign=bottom> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="https://www.getinge-training.com/images/signature.jpg" width="345" height="110" border="0"></td> </tr>
</table>
</body>
</html>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 03, 2022 Mar 03, 2022

I would merely repeat my last comment. Please reconsider it. 


/Charlie (troubleshooter, carehart. org)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 03, 2022 Mar 03, 2022

@Sandhya231275905kpl , l shall say it again: all of these attempts (with an HTML table, cfheader with Word Content-Disposition, styles, etc.) will NOT produce the display you want. It is well-nigh impossible for the margins that you set in HTML to carry over to MS Word.

 

Imagine you wish to frame a picture. Obviously, the dimensions and aspect-ratio of the frame and mat board will be the factors that determine how well your picture will fit.

BKBK_0-1646342945696.png

You may be able to adapt the picture's dimensions. But there is a limit to how the picture can affect the mat board and frame. In fact, the frame is fixed and determines the required size and aspect ratio of the picture, not vice versa.

 

This analogy carries over to your code. The HTML output corresponds to the picture, with or without mat board.  MS Word (independent software unrelated to CF) supplies the fixed frame. MS Word may supply headers, footers or margins that you cannot anticipate in the HTML. 

 

What your HTML code is doing is, simply, streaming HTML output to the browser. The Content-Disposition header tells the browser to offer the content as a downloadable MS Word file rather than display it inline.

 

That's it. Just as the picture in our frame analogy has limited influence on the dimensions of the frame and mat board, so too HTML has limited influence on the footers, headers, margins, styling, etc. that MS Word may impose.

 

What to do then?

 

Your best bet is to avoid the table tag. Use divs instead.

A simple example to demonstrate:

 

<html>
<head>
<cfheader name="content-disposition" value="attachment; filename=1.doc">
</head>
<body style="margin-top: 0in">
<div style="margin-left:-1.0in;margin-right:-1.0in;">
	<img src="http://localhost:8500/workspace/CF_Project/TopHeader.jpg" border="0" width="1429" height="496" style="max-width:100%;">
</div>
</body>
</html>

 

  1. Open the downloaded file 1.doc in MS Word.
  2.  Click on Page Layout, then on Margins.
  3.  Click on Custom Margins, select 0 as Top margin and press OK.
  4.  Examine the Print Preview and you will see that the image is at the top margin of the page.
    BKBK_0-1646347903324.png

     

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 03, 2022 Mar 03, 2022

If the automated generation of this document (a certificate) is as important to you as I think it is, then you should use a specialist library to generate the doc or docx file. For example, Apache POI.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2022 Mar 04, 2022
LATEST

I have yet another suggestion. Use the method that most learning institutions use to print their certificates.

 

  1.  Get a professional printing service to design and print a page containing the static elements of the certificate. By static elements I am referring to page elements that remain the same for all certificants. Think, for example, of the TopHeader picture, static text, the Signature picture and perhaps a background picture, a watermark or embossing. Each should be placed at the exact position on the page. The appropriate page format (A4, Legal, Letter, etc.) and fonts should also be chosen .
    The static elements should be positioned such as to make allowance for the variety of the certificant-data to be filled in.
  2. Have the printer generate scores, hundreds or thousands of copies of the page, depending on the number of certificates you will need to generate.
  3.  Write CFML code to generate and place the dynamic elements of each certificate on the page. By dynamic elements I am referring to the name and credential of the certificant as well as the city, state and date. 

 

I consider that to be the best solution.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources