简体   繁体   English

如何使用PDFMARK和/或PERL cpan模块PDF :: API2创建PDF表单?

[英]How can I create a PDF form using PDFMARK and/or PERL cpan module PDF::API2?

Does anyone have any sample that demonstrates how one may create a simple PDF form (single text input box, or single radio button) and 'submit field_name/value pairs via HTTP post' to server when clicked, using either of three methods? 是否有人有示例说明使用三种方法之一可以如何创建简单的PDF表单(单个文本输入框或单个单选按钮)以及如何通过单击“通过HTTP发布提交field_name /值对”到服务器?

  1. By embedding Adobe's PDFMARK comments into a Postscript file, then convert the Postscript file to PDF via Adobe distiller or ghostscript? 通过将Adobe的PDFMARK注释嵌入到Postscript文件中,然后通过Adobe蒸馏器或ghostscript将Postscript文件转换为PDF? I read the PDFMARK documentation and it only briefly mentions one 'action' can be sent to 'form submit'. 我阅读了PDFMARK文档,仅简要提及可以将一个“操作”发送到“表单提交”。 but the docs are sorely lacking in true detail of that implementation. 但是文档非常缺少该实现的真实细节。 Usually Adobe's docs are very great, but the author must have been pressed to finish that chapter before he actually finished it. 通常,Adobe的文档非常出色,但是在实际完成本章之前,必须要求作者完成该章。 Lacking in the details is that PDF forms may actually submit up to four different types of packets to a server, none of this is mentioned. 缺少的细节是PDF表单实际上可以向服务器提交多达四种不同类型的数据包,但都没有提及。 As well, the actual syntax of the 'form submit' action is not even presented. 同样,甚至没有提供“表单提交”操作的实际语法。
  2. By using PERL and the free PDF::API2 or 3 PDF manipulation library. 通过使用PERL和免费的PDF::API2或3 PDF操作库。 The author has made available low level function calls that he believes allows enough possibility to actually achieve this but alas, has not actually used his library to do this specific task. 作者提供了一些低级函数调用,他认为这些函数调用有足够的可能性实际实现此目标,但是,还没有实际使用他的库来完成此特定任务。 Has anyone out there figured out how to do this with his library? 有没有人想出如何用他的图书馆做到这一点? I ask as I am primarily a programmer in Perl. 我问,因为我主要是Perl的程序员。
  3. Finally, if neither of above yield anything to me, does anyone know of ANY windows command line callable method to do this using free or relatively low cost software (less than $200). 最后,如果以上两种方法都不对我产生任何帮助,那么有谁知道有任何Windows命令行可调用方法使用免费或相对低成本的软件(不到200美元)来实现。 I would be willing to learn another language if this may be achieved using a different development environment. 如果可以使用其他开发环境来实现,我将愿意学习另一种语言。

Using pdfmark in a PostScript language program 在PostScript语言程序中使用pdfmark

First, I'll tackle part 1 of your question: creating such a PDF form file, by running a PostScript language program with pdfmark expressions through Adobe Acrobat Distiller or the like. 首先,我将解决您的问题的第1部分:通过Adobe Acrobat Distiller等通过运行带有pdfmark表达式的PostScript语言程序来创建这样的PDF表单文件。 See the sample program below. 请参见下面的示例程序。

The "PDFMARK documentation" which you read was the pdfmark Reference , I take it. 您阅读的“ PDFMARK文档”是pdfmark参考 ,我接受了。 My copy is dated November 2006. The code you need is almost all right there, in Examples 5.11 Define the AcroForm dictionary at the document Catalog and 5.12 Define the Widget annotations, which are also field dictionaries for this form , on pages 70-75. 我的副本的日期是2006年11月。在示例5.11中,几乎所有需要的代码都在这里,在文档目录中定义AcroForm字典 ,在示例5.12中, 定义小部件注释,这也是此表单的字段字典 ,在第70-75页。 Read the code there carefully, copy it into a source file and distill it, modify to suit. 仔细阅读那里的代码,将其复制到源文件中并进行提炼,然后进行修改以适应需要。 That's what my sample is based on. 这就是我的样本所基于的。

Be sure to read carefully Chapter 1 Introduction (p.9-16), which will help you read the pdfmark syntax in Examples 5.11 and 5.12. 请务必仔细阅读第1章简介 (第9-16页),这将帮助您阅读示例5.11和5.12中的pdfmark语法。 The section Named objects (p. 12-16) is important for understanding how the fonts and encodings in the examples are referenced. 命名对象 ”一节(第12-16页)对于理解如何引用示例中的字体和编码很重要。

But you also need to read the PDF language specification. 但是您还需要阅读PDF语言规范。 Adobe's copy of the spec has been superseded by the ISO standard PDF 32000-1:2008 Document management — Portable document format — Part 1: PDF 1.7 Read Section 12.7 Interactive Forms (pp. 430-456), which will frequently refer you to Section 12.5 Annotations (pp. 381-414) for controls on the appearance of form fields. ISO规范的Adobe副本已被ISO标准PDF 32000-1:2008取代。 文档管理—便携式文档格式—第1部分:PDF 1.7阅读第12.7节“ 交互式表格” (第430-456页),该表格经常将您引向本节。 12.5用于控制表单域外观的注释 (第381-414页)。

The important thing to understand is that most the form fields will be defined by key-value pairs delivered in a PostScript language [ ... /ANN pdfmark expressions. 要了解的重要一点是,大多数表单字段将由以PostScript语言[ ... /ANN pdfmark表达式提供的键值对定义。 The key-value pairs are specified in the PDF specification, not the pdfmark specification. 键值对在PDF规范中指定,而不是在pdfmark规范中指定。 But it's easy to edit the pdfmark expressions while reading the PDF specification. 但是在阅读PDF规范时很容易编辑pdfmark表达式。

The following program distills into a PDF file with form fields: 以下程序提取为带有表单字段的PDF文件:

% Simple pdfmark form example
% by Jim DeLaHunt, jdlh.com, 3. March 2012
%
% This PostScript language code, when converted to PDF by 
% Adobe Acrobat Distiller or the like, creates a PDF file with form
% fields, and a Submit button which sends the values of the fields
% via an HTTP POST request.
%
% In reply to Stack Overflow question, "How can I create a PDF form 
% using PDFMARK and/or PERL cpan module PDF::API2?",
% http://stackoverflow.com/questions/9440930/
%

% Define stubs for interpreters without pdfmark support
% From pdfMark reference, Example 1.2, p.10
/pdfmark where {
    % pdfmark is built-in
    % define {code} ?pdfmark - : exec modal PDF code.
    pop globaldict /?pdfmark /exec load put
}{
    globaldict begin
        /?pdfmark /pop load def% pdfmark is absent: ignore code.
        /pdfmark /cleartomark load def
    end
} ifelse
%%EndProlog

% This code makes the example clearer. It isn't part of the AcroForm.
<</PageSize [300 300]>> setpagedevice  % smaller page (~ 10cm x 10cm)

clippath 0.9 setgray fill  % light grey background, to show widgets.
/Helvetica-Bold 12 selectfont 0 setgray  % for labels
/showlabel {   % (text) x y showlabel - 
    % shows (text) top-justified at x,y, moves down one line
    -12 add moveto   % x y -12 add moveto -
    gsave show grestore
    0 -3 rmoveto  % leading (space between lines)
} def
/showcomment {  % (text) showcomment - 
    % shows (text) top-justified at current point, moves down 1 line
    0 -10 rmoveto 
    gsave /Helvetica 10 selectfont show grestore
    0 -2 rmoveto  % leading (space between lines)
} def

(Simple pdfmark form example) 10 290 showlabel


% AcroForm dictionary setup. From pdfmark Reference, Example 5.11, p.70
%
% Read also PDF specification PDF32000:2008 
% "Document management — Portable document format — Part 1: PDF 1.7",
% Section 12.7 "Interactive Forms", p.430
% To understand the syntax: [/_objdef {foo} /type /dict /OBJ pdfmark
% read carefully pdfMark reference, "User-defined named objects", p.13.

% Define font objects for the widgets to use
[ /_objdef {ZaDb} /type /dict /OBJ pdfmark
[ {ZaDb} <<
    /Type /Font
    /Subtype /Type1
    /Name /ZaDb
    /BaseFont /ZapfDingbats
>> /PUT pdfmark
[ /_objdef {Helv} /type /dict /OBJ pdfmark
[ {Helv} <<
    /Type /Font
    /Subtype /Type1
    /Name /Helv
    /BaseFont /Helvetica
    % /Encoding {pdfDocEncoding} % for simplicity, use font's own encoding
    % pdfmark Reference, Example 5.11, p.70, provides an Encoding
    % array defining PDFDocEncoding
>> /PUT pdfmark

[ /_objdef {aform} /type /dict /OBJ pdfmark

% Define Fields array of Acroform dictionary. It will contain entries for
% each of the widgets defined below.
% NOTE: It is not necessary to explicitly assign the widget annotations
% to the Fields array; Acrobat does it automatically when the file is
% opened.  

[ /_objdef {afields} /type /array /OBJ pdfmark
[ {aform} <<
    /Fields {afields}
    /DR << /Font << /ZaDb {ZaDb} /Helv {Helv} >> >>
    /DA (/Helv 0 Tf 0 g)
    /NeedAppearances true
    % "/NeedAppearances true" tells reader 'to construct appearance
    % streams and appearance dictionaries for all widget annotations
    % in the document...' --PDF32000:2008 Table 218 p.431
>> /PUT pdfmark

% Put Acroform entry in catalog dictionary
[ {Catalog} << /AcroForm {aform} >> /PUT pdfmark


% Example 5.12 Define the Widget annotations
% pdfmark Reference pp. 72-75, with clarifications by Jim DeLaHunt
%
% These Widget annotations are also field dictionaries for this form.
% This is the collection of all individual widget annotations. 
% It is possible to have multiple instances of these sections, such as
% for defining a single widget on each instance.
%
% See PDF32000:2008 Section 12.5.6.19 "Widget Annotations", p.408
% especially tables 188 and 189.

(/Tx text field) 10 260 showlabel
(with Javascript) showcomment (validation) showcomment
[ /Subtype /Widget
    /Rect [100 223 245 260]
    /F 4
    /T (SL Text)
    /FT /Tx
    /DA (/Helv 14 Tf 0 0 1 rg)
    /V (5)
    /AA <<
        /K << 
            /S /JavaScript 
            /JS (AFNumber_Keystroke\(2, 0, 0, 0, "$", true\);)
        >>
        /F << 
            /S /JavaScript 
            /JS (AFNumber_Format\(2, 0, 0, 0, "$", true\);)
            % pdfmark Reference p.72 seems to omit closing ")" above. Oops.
        >>
    >>
/ANN pdfmark

(/Btn field) 10 208 showlabel
(Check Box) showcomment
[  /Subtype /Widget
    /Rect [100 172 136 208]
    /F 4
    /T (Check Box)
    /FT /Btn
    /DA (/ZaDb 0 Tf 0 g)
    /AS /Off
    /MK << /CA (4)>>
    /AP << /N << /Oui /null >> >>
/ANN pdfmark

(/Ch field) 10 160 showlabel
(List Box) showcomment
[   /Subtype /Widget
    /Rect [100 87 137 160]
    /F 4
    /T (List Box)
    /FT /Ch
    /DA (/Helv 10 Tf 1 0 0 rg)
    /Opt [(1)(2)(3)(4)(5)]
    /DV (3)
    /V (3)
    /MK <<
        /BG [1 1 1]   % Background: white
        /BC [0 0 0]   % Border colour: black
    >>
    % MK is 'An appearance characteristics dictionary (see Table 189)'
    % --PDF32000:2008 Table 188 p.408
    % Without an /MK entry, Acrobat Pro 8.0 complains of an error
    % when displaying this field, and some entries aren't displayed.

/ANN pdfmark

(/Btn button) 160 85 showlabel
(/S /SubmitForm action) showcomment
% Example of how the /MK dictionary is used.
[   /Subtype /Widget
    /Rect [ 130 10 270 50 ]
    /F 4
    /T (Submit)
    /FT /Btn
    /H /P
    /A << 
        /S /SubmitForm 
        /F (http://posttestserver.com/post.php) % Thanks, Henry!
        /Flags 16#6 % IncludeNoValueFields, ExportFormat
    >>
    /DA (/HeBo 18 Tf 0 0 1 rg)
    /Ff 65536
    /MK <<
        /BC [ 1 0 0 ]
        /BG [ 0.75 0.45 0.75 ]
        /CA (Submit)
        /AC (Submitted!)
    >>
    /BS <<
        /W 3
        /S /I
    >>
/ANN pdfmark

% When pressing the Submit button in Adobe Acrobat Professional 8.3.1,
% there is a pause, and then Acrobat displays a PDF file with the
% results of the HTTP POST operation. This is text like,
%   Successfully dumped 3 post variables. View it at
%   http://www.posttestserver.com/data/2012/03/04/01.29.271751566025 
%   Post body was 0 chars long.
%
% Browse to that URL, and you will see diagnostics with the following:
%   ....
%   Post Params:
%   key: 'Check_Box' value: 'Oui'
%   key: 'List_Box' value: '4'
%   key: 'SL_Text' value: '3.14'
%   Empty post body.

showpage  % make sure a page gets produced

Open the resulting PDF document in a PDF reader. 在PDF阅读器中打开生成的PDF文档。 Change the values in the forms. 更改表单中的值。 Note that the top text field limits you to a number with two decimal places, and it places a $ sign in front of the number. 请注意,顶部的文本字段将您限制为带两位小数的数字,并且在该数字前放置$符号。 It does this with Javascript code. 它使用Javascript代码执行此操作。 There is a checkbox, which sends the value Oui when checked. 有一个复选框,选中该复选框时将发送值Oui

When you click on the Submit button, the PDF reader takes the keys for each form field and the value of that field, and sends them to a URL via an HTTP POST call. 当您单击Submit按钮时,PDF阅读器将获取每个表单字段的密钥和该字段的值,然后通过HTTP POST调用将其发送到URL。 The destination I used is Henry's HTTP Post Dumping Server . 我使用的目的地是Henry的HTTP Post Dumping Server The server returns a text reply to the POST, giving a URL where you can see what the server received. 服务器返回对POST的文本答复,并提供一个URL,您可以在其中查看服务器收到的内容。 Adobe Acrobat Professional turns this reply into a PDF file, which may take a few seconds. Adobe Acrobat Professional将此答复转换为PDF文件,这可能需要几秒钟。 Visit that URL, and you can see the field_name/value pairs you seek. 访问该URL,您可以看到您要查找的field_name / value对。

Using PERL module PDF::API2 使用PERL模块PDF :: API2

I'm not very experienced with Perl, and I've never used the PDF::API2 module . 我对Perl不太熟悉,并且从未使用过PDF :: API2模块 However, from a code inspection I suspect it will not be straightforward to generate PDF forms with PDF::API2. 但是,从代码检查来看,我怀疑用PDF :: API2生成PDF表单并不是一件容易的事。

The PDF::API2::Annot sub-module does look promising. PDF :: API2 :: Annot子模块看起来确实很有希望。 But in order to use the method of the example program above, one must create PDF Annotations of /Subtype /Widget . 但是,为了使用上述示例程序的方法,必须创建/Subtype /Widget PDF注释。 It appears PDF::API2::Annot does not support this. 似乎PDF :: API2 :: Annot不支持此功能。

Looking through the source code of PDF::API2::Annot , one can see examples of how it handles other Annotation types. 查看PDF :: API2 :: Annot源代码 ,可以看到有关其如何处理其他Annotation类型的示例。 For example, the Link annotation type is created in part by code like: 例如,链接批注类型部分由类似以下代码的代码创建:

$self->{Subtype}=PDFName('Link');

Were Widget annotations to be supported, one would expect to see statements like $self->{Subtype}=PDFName('Widget') . 如果要支持Widget注释,可能希望看到类似$self->{Subtype}=PDFName('Widget')语句。 But there is no instance of the string Widget in this module (version 2.019). 但是此模块(版本2.019)中没有字符串Widget实例。 Hence, it will likely take an extension to this module to support Widget annotations. 因此,可能需要对该模块进行扩展以支持Widget注释。

Now Annotations are only PDF dictionaries with specific contents. 现在,“注释”只是具有特定内容的PDF词典。 It is probably possible to use PDF::API2::Basic::PDF::Dict to construct Widget annotations from basic parts. 可能可以使用PDF :: API2 :: Basic :: PDF :: Dict从基本部分构造Widget注释。 However, I'm guessing that this would be even more work than extending PDF::API2::Annot . 但是,我猜测这将比扩展PDF :: API2 :: Annot还要耗费更多精力。

Take this question, and the bounty it attracted, as a sign: there will be much rejoicing if someone adds Widget annotation support to PDF::API2::Annot , and perhaps makes a PDF::API2::Form module! 以这个问题及其吸引的赏金为标志:如果有人将Widget注释支持添加到PDF :: API2 :: Annot ,并且可能制作了PDF :: API2 :: Form模块,将会带来很多欢乐!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM