欢迎光临
我们一直在努力

用asp生成PDF文件

建站超值云服务器,限时71元/月

creating a pdf with asp

by ty button

print this article

email this article to a colleague

introduction

adobes 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 were 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 didnt 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 didnt really like working with the validation, etc., and then would still have to figure out how to incorporate the database calls. this wasnt 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, youll find that all the source code is included in vb and c++. were 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, were set to go.

step 3. create the form.

well 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 youve 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. were 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

%>

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 oregons 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.

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用asp生成PDF文件
分享到: 更多 (0)

相关推荐

  • 暂无文章