CSDN博客

img zhongjiannet

巧用ASP生成PDF文件

发表于2004/6/23 10:23:00  723人阅读

Adobe PDF格式已经变成很多机构和公司进行跨平台制表的通用媒体格式。尽管我不是这个产品的狂热痴迷者,却不得不接受这样一个事实:用这个格式产生一个协定可能会比用Word还要好。
     
    由于在一家雇用公司工作,我已经把我们所有早期的软件写成能在Internet上运行,这样我们就能和小的子公司共享公用数据库了。然而我们遇到的最大的问题就是制表。当一个申请人坐在信息站里在线填写他们的就业申请时,我们如何取得当前的文档(或申请等)?由于我们做了大量工作来获取数据,我们还需要申请人手工填写一份申请。
     
    我试了各种办法,但是看来都有一个很大的限制:
     
    如果我能快速产生它,它就只能是一个HTML形式的衍生物,在打印时就会变得面目全非。如果设计的看起来美观一些,它就会变得很慢。这是由于我选择用RTF格式作为我的首选项,然后使用文件系统对象来完成基于模板的RTF文件,最后加入了我的信息。这个磁盘读写的过程会耗费很长时间。记住:我们这里不是在讨论列表数据或者一个Excel电子表格。我们是想让程序用我们自己的标志进行处理。
     
    最后,我选择了Adobe(很像一句广告词?——译者)。我讨厌它的浏览器所以希望自己不会和它纠缠上,然而它的格式看起来很不错而且文件也不大。大部分用户都在系统里装上它,我们的客户也不例外。
     
    因此我一直在寻找把数据从HTML表单传递到PDF文件的方法。我试过使用Adobe表单,但是它不能真正有效运行,然后还要统计如何合并数据库请求。这根本不是我想要的东西,我想直接把数据从我的HTML表单传递到数据库,然后产生数据信息到PDF文件里作为浏览或打印。
    最终我在UseNet发现了线索。一个叫Jeremy Hunter的提交的一个链接包含了我准备在这里讲的很多东西——http://partners.adobe.com/asn/developer/acrosdk/forms.html
     
    首先必须在定义字段的工作站(不是服务器)上安装有Adobe完全版。这个是一个指向Adobe Forms Acrobat Toolkit的链接,由此开始一切就容易了。
     
     
    第一、下载这个套装工具并把它解压缩到指定目录。
     
    当你进行这一步操作时,你会发现所有源代码都在VB或VC++中。我们这里不打算关注它,但是如果你需要代码就在这里。
     
     
    第二、注册你的DLL。
     
    有两个DLL需要被复制到你的服务器上。一个在Visual Basic文件夹中叫fdfacx.dll,另一个在Visual C++文件夹中叫fdftk.dll。用regsvr32注册fdfacx.dll文件。我把这两个文件都放到c:winntsystem32下,并在当前路径下输入“regsvr32 fdftk.dll”。如果你遇到错误提示信息,请检查确认这两个文件确实在当前目录下,没有问题则进入下一步。
     
     
    第三、建立表单。
     
    我们将使用每个人对这个示范都很熟悉的表单。这在“http://www.irs.gov”随意可得并为我们提供足够的领域来展示我们能作的东西。
     
     
    第四、定义Adobe表单字段。
     
    在Adobe Acrobat中打开文档后选择表单工具,描述你的第一个表单字段(First Name)。
     
    在你定义后它将让你命名,我这里命名为“FirstName”。注意这里有几个标准的附加格式选项,除了字体大小,我选择进行合法性检查和格式化我的ASP文档。由于这是一个快速演示,我选择跳过任何检查,但是我将在客户端的表单进行处理,并在传递变量前在服务端格式化ASP页面。
     
    继续其他字段,每一个都要注意命名并可能格式化字体大小或属性。
    在服务器上保存你的修改。
     
    第五、编写ASP页面。
     
    在开始这一步之前你要注意到FDF套装工具阐述的一个主要对象——“FDFApp.FDFApp”。它开放了许多方法,手册上概述了其他的可能方法。我们主要关心两个方法——fdfSetValue和fdfSetFile。接下来切入正题。  
               
    第六、没有必要成为PDF专家,这是我目前的建议。套装工具的用户指导概述了所有方法,看上去在这个入门介绍之外还有很多可能的方法。就像你所看到的一样,你可以很容易添加数据库命令来收集数据或向数据库记录数据。
     
    原作者简介:
    Ty Button就职于俄勒冈的一个名叫Cardinal Services的雇用公司。Cardinal Services公司曾被俄勒冈商业杂志评为1997和1998年度俄勒冈南部第一号雇用公司,同时被南海岸商业开发中心和美国银行评为俄勒冈发展最快的公司。Button是MIS开发人员,也在网络数据库开发和生产过程自动化上花费了大部分时间

以下是英文原文

Creating a PDF with ASP
By Ty Button

print this article

email this article to a colleague

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:/winnt/system32/ 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", "X", false
End If
If FilingStatus = 3 Then
 MyFdf.fdfsetValue "MarriedBut", "X", 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:/inetpub/wwwroot/workorder/CheckThis.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
%>

0 0

相关博文

我的热门文章

img
取 消
img