用asp生成PDF文件(英文)

Creating a PDF with ASP

By Ty Button

Introduction
Adobe''s PDF format has become the lingua franca of cross-platform reporting for many agencies and companies. While I was no great fan of the product,I have to admit it probably does a better job of producing a compact document with loads of formatting than Word ever will. 
Getting Started
Working for a staffing firm,I have written all of our front-end software to run over the Internet so we can share common databases with our smaller branch offices. The biggest problem we faced,however,was reporting. How do we get live documents (applications,etc.) to generate themselves when an applicant sits down at the kiosk and fills out their on-line employment application? While we''re doing a great job of capturing the data,we still need an applicant to sign the application,W-4,etc.
I tried a number of things,but the limits seem to be:

If I could produce it fast,it was an HTML form derivative and looked terrible when printed.
If I could produce it looking right,it was clunky and slow. This is because I settled on RTF as my best initial option and ended up using the File System Object to write RTF files based on a template and parse my info into them. The disk reads and writes took their toll.
Remember,we抮e not talking about tabular data or an Excel spreadsheet. We want the application with our logo to be processed.

Finally,I settled on Adobe. I hate the viewer and wish I didn''t have to mess with it. However,the formatting looks good and the files are thin. Most users have it on their system,and all my users do.

So I searched and searched for ways to pass data from HTML forms to PDF files. I tried using Adobe forms,but didn''t really like working with the validation,etc.,and then would still have to figure out how to incorporate the database calls. This wasn''t at all what I wanted. I wanted to pass data directly from my HTML form to a database while generating the field data into the PDF file for display or printing.

Finally,there was a clue on UseNet. A link posted by Jeremy Hunter contained much of what I will address here. http://partners.adobe.com/asn/developer/acrosdk/forms.html

REQUIRED SOFTWARE
ADOBE (the full version) is required on the workstation defining the fields. (Not the server)

This is the link to the Adobe Forms Acrobat Toolkit. From there it was easy going.

Step 1. Download the toolkit and unzip it to your directory of choice.

When you do,you''ll find that all the source code is included in VB and C++. We''re not going to worry about that here,but the code is there if you need it.

Step 2. Register your dlls.

Two dlls need to be copied to your server. The first is in the Visual Basic sub folder and is named fdfacx.dll. The second is in the Visual C subfolder and is named fdftk.dll. Run refsvr32 on the fdfacx.dll file. It will take care of registering the other itself. I put both in c:winntsystem32 and from that directory typed regsvr32 fdftk.dll. If you get an error message,check to make sure both files are really there. If not,we''re set to go.

Step 3. Create the form.

We''ll use a form everyone is too familiar with for this demonstration - the W-4. This one is readily available on http://www.irs.gov and gives us enough fields to show what we can do.


<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<H1>Adobe FDF Example</H1>
<FORM NAME=W4Help ACTION=W4.asp METHOD = POST>
<TABLE>
 <TR>
  <TD ALIGN=RIGHT VALIGN=TOP>First Name</TD>
  <TD><INPUT TYPE=TEXT NAME=txtFirstName>
  </TD>
 </TR>
 <TR>
  <TD ALIGN=RIGHT VALIGN=TOP>Middle Initial
  </TD>
  <TD><INPUT TYPE=TEXT NAME=txtMI>
  </TD>
 </TR>
 <TR>
  <TD ALIGN=RIGHT VALIGN=TOP>LastName</TD>
  <TD><INPUT TYPE=TEXT NAME=txtLastName>
</TD>
 </TR>
  <TR>
  <TD ALIGN=RIGHT VALIGN=TOP>Social</TD>
  <TD><INPUT TYPE=TEXT NAME=txtSocial1 SIZE=3>-
<INPUT TYPE=TEXT NAME=txtSocial2 SIZE=2>-
<INPUT TYPE=TEXT NAME=txtSocial3 SIZE=2></TD>
 </TR>
 <TR>
  <TD ALIGN=RIGHT VALIGN=TOP>Street Address</TD>
  <TD><INPUT TYPE=TEXT NAME=txtStreetAddress></TD>
 </TR>
 <TR>
  <TD ALIGN=RIGHT VALIGN=TOP>City
  </TD>
  <TD><INPUT TYPE=TEXT NAME=txtCity></TD>
 </TR>
 <TR>
  <TD ALIGN=RIGHT VALIGN=TOP>State</TD>
  <TD><INPUT TYPE=TEXT NAME=txtState SIZE=2 MAXLENGTH=2></TD>
 </TR>
 <TR>
  <TD ALIGN=RIGHT VALIGN=TOP>Zip</TD>
  <TD><INPUT TYPE=TEXT NAME=txtZip SIZE=10></TD>
 </TR>
 <TR>
  <TD ALIGN=RIGHT VALIGN=TOP>Filing Status</TD>
  <TD>
  <INPUT TYPE=RADIO NAME=radFilingStatus VALUE="1">Single
  <BR>
  <INPUT TYPE=RADIO NAME=radFilingStatus VALUE="2">Married
  <BR>
  <INPUT TYPE=RADIO NAME=radFilingStatus VALUE="3">
  Married but withholding at the higher single rate.
  <BR></TD>
 </TR>
 <TR>
  <TD ALIGN=RIGHT VALIGN=TOP>Allowances Claimed</TD>
  <TD><INPUT TYPE=TEXT NAME=txtAllowances SIZE=2 MAXLENGTH=2> </TD>
 </TR>
 <TR>
  <TD ALIGN=RIGHT VALIGN=TOP>Additional amount to withhold (if any)</TD>
  <TD><INPUT TYPE=TEXT NAME=txtAdditional SIZE=2 MAXLENGTH=2>
  </TD>
 </TR>
 <TR>
  <TD ALIGN=RIGHT VALIGN=TOP>I want to file Exempt from Withholding
  </TD>
  <TD><INPUT TYPE=CHECKBOX NAME=chkExempt></TD>
 </TR>
</TABLE>
<INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>


Step 4. Define the Adobe form fields.


Open the document in Adobe Acrobat.
Click the form tool.
Paint your first form field (First Name).
As soon as you''ve defined it you are asked to name it. I named mine FirstName. Note that there are several additional formatting options. Other than font size,I choose to do my validation and formatting in my ASP document. As this is just a quick demo,I have opted to skip any validation. However,I would handle it on the client side in my form and any formatting server side in my ASP page before I pass the variables.
Continue with the remaining fields,naming each and possibly formatting the font size or attributes.
Save your work to wherever you want on your Web server.
Step 5. Write the ASP page.

Before you start,realize that there is one main object exposed by the FDF toolkit - "FDFApp.FDFApp." There are many methods exposed,and the manual outlines some other possibilities. We''re mainly concerned with two methods,fdfSetValue and fdfSetFile. Here we go,


<%@ Language=VBScript %>
<% Response.Buffer = true%>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<%
''
'' Retrieve the user responses
''
FirstName = Request.form("txtFirstName")
MI = Request.form("txtMI")
LastName = Request.form("txtLastName")
SS1 = Request.form("txtSocial1")
SS2 = Request.form("txtSocial2")
SS3 = Request.form("txtSocial3")
StreetAddress = Request.form("txtStreetAddress")
City = Request.form("txtCity")
State = Request.form("txtState")
Zip = Request.form("txtZip")
FilingStatus = Request.form("radFilingStatus")
Allowances = Request.form("txtAllowances")
Additional = Request.form("txtAdditional")
Exempt = Request.form("chkExempt")
If Exempt = "on" Then
 Exempt = "EXEMPT"
Else
 Exempt = ""
End If
''
'' Create an instance of the Object
''
Set FdfAcx = Server.CreateObject("FdfApp.FdfApp")
''
''  Use the fdfApp to feed the vars
''
Set myFdf = FdfAcx.FDFCreate
''
'' Stuff the variables
''
myFdf.fdfsetvalue "FirstName",FirstName,false
myFdf.fdfsetvalue "MI",MI,false
myFdf.fdfsetvalue "LastName",LastName,false
myFdf.fdfsetvalue "SS1",SS1,false
myFdf.fdfsetvalue "SS2",SS2,false
myFdf.fdfsetvalue "SS3",SS3,false
myFdf.fdfsetvalue "StreetAddress",StreetAddress,false
myFdf.fdfsetvalue "City",City,false
myFdf.fdfsetvalue "State",State,false
myFdf.fdfsetvalue "Zip",Zip,false
If FilingStatus = 1 Then
 MyFdf.fdfsetValue "StatusSingle","X",false
End If
If FilingStatus = 2 Then
 MyFdf.fdfsetValue "StatusMarried",false
End If
If FilingStatus = 3 Then
 MyFdf.fdfsetValue "MarriedBut",false
End If
myFdf.fdfsetvalue "Allowances",Allowances,false
myFdf.fdfsetvalue "Additional",Additional,false
myFdf.fdfsetvalue "Exempt",Exempt,false
''
'' Point to your pdf file
''
myFDF.fdfSetFile "http://www.servername.com/workorder/w4.pdf"
Response.ContentType = "text/html"
''
'' Save it to a file.  If you were going to save the actual file past the point of printing
'' You would want to create a naming convention (perhaps using social in the name)
'' Have to use the physical path so you may need to incorporate Server.mapPath in
'' on this portion.
''
myFDf.FDFSaveToFile "C:inetpubwwwrootworkorderCheckThis.fdf"
'' Now put a link to the file on your page.
Response.Write "<a href=http://www.servername.com/workorder/CheckThis.fdf>pdf</A>"
''
'' Close your Objects
''
myfdf.fdfclose
set fdfacx = nothing
%>


 
Conclusion
Without having to be an expert at FDF or PDF,this is my answer for the moment. The users?guide that comes with the toolkit outlines all of the methods and looks like it has many possibilities beyond this rudimentary introduction. As you can see,you could just as easily add database commands to collect the data and log it to a database. 
About the Author
Ty Button is employed by Cardinal Services,Inc.,a staffing firm in Oregon. Cardinal Services has been named the #1 Staffing Firm in Southern Oregon for both 1997 and 1998 by Oregon Business Magazine,as well as Oregon''s Fastest Planned Growth Company by the South Coast Business Development Center and U.S. Bank. Button is the MIS,but spends the bulk of his time on Web-database development and process automation. He can be reached at mailto:ty@ty-button.com

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


数组的定义 Dim MyArray MyArray = Array(1‚5‚123‚12‚98) 可扩展数组 Dim MyArray() for i = 0 to 10
\'参数: \'code:要检测的代码 \'leixing:html或者ubb \'nopic:代码没有图片时默认值
演示效果: 代码下载: 点击下载
环境:winxp sp2 ,mysql5.0.18,mysql odbc 3.51 driver 表采用 myisam引擎。access 2003  不同的地方: 
其实说起AJAX的初级应用是非常简单的,通俗的说就是客户端(javascript)与服务端(asp或php等)脚本语言的数据交互。
<% ’判断文件名是否合法 Function isFilename(aFilename)  Dim sErrorStr,iNameLength,i  isFilename=TRUE
在调用的时候加入判断就行了. {aspcms:navlist type=0 } {if:[navlist:i]<6} < li><a href=\"[navlist:link]\" target=\"_top\">[navlist:name]</a> </li>
导航栏调用 {aspcms:navlist type=0}     <a href=\"[navlist:link]\">[navlist:name]</a>
1.引入外部文件: {aspcms:template src=infobar.html} 2.二级下拉菜单 <ul class=\"nav\">
downpic.asp页面:  <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
Cookies是数据包,可以让网页具有记忆功能,在某台电脑上记忆一定的信息。Cookies的工作原理是,第一次由服务器端写入到客户端的系统中。以后每次访问这个网页,都是先由客户端将Cookies发送到服务器端,再由服务器端
很简单,在需要调用的地方用这种模式 {aspcms:content sort={aspcms:sortid} num=17 order=isrecommend}
网站系统使用ACCESS数据库时,查询时怎么比较日期和时间呢?为什么常常比较出来却是错误的呢?比如早的日期比迟的日期大?
str1=\"1235,12,23,34,123,21,56,74,1232\" str2=\"12\" 问题:如何判断str2是否存在str1中,要求准确找出12,不能找出str1中的1235、123、1232
实例为最新版本的kindeditor 4.1.5. 主要程序: <% Const sFileExt=\"jpg|gif|bmp|png\" Function ReplaceRemoteUrl(sHTML,sSaveFilePath,sFileExt)
用ASP实现搜索引擎的功能是一件很方便的事,可是,如何实现类似3721的智能搜索呢?比如,当在搜索条件框内输入“中国人民”时,自动从中提取“中国”、“人民”等关键字并在数据库内进行搜索。看完本文后,你就可以发
首先感谢ASPCMS官网注册用户xing0203的辛苦付出!一下为久忆YK网络转载原创作者xing0203的文章内容!为了让小白更加清楚的体验替换过程,久忆YK对原文稍作了修改!
数据库连接: <% set conn=server.createobject(\"adodb.connection\") conn.open \"driver={microsoft access driver (*.mdb)};dbq=\"&server.mappath(\"数据库名\")
第1步:修改plugins下的image/image.js 找到\'<input type=\"button\" class=\"ke-upload-button\" value=\"\' + lang.upload + \'\" />\',
asp函数: <% Const sFileExt=\"jpg|gif|bmp|png\" Function ReplaceRemoteUrl(sHTML,sSaveFilePath,sFileExt)