将XFDF与PDF表单合并以创建最终的PDF服务器端? [英] merging XFDF with PDF form to create final PDF server-side?

查看:136
本文介绍了将XFDF与PDF表单合并以创建最终的PDF服务器端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前拥有的:

  • 用户提交表单数据并获得下载PDF"链接.

  • User submits form data and gets a "download PDF" link.

该链接指向脚本,该脚本可动态生成XFDF文件,并在设置适当的标头等之后输出XFDF文件.

The link points to script which generates an XFDF file on the fly and outputs the XFDF file after setting the appropriate headers, etc.

XFDF文件指向受密码保护的PDF,这是使用XFDF数据填写字段的通用PDF表单.

The XFDF file points to a password-protected PDF, which is the generic PDF form that uses the XFDF data to fill in the fields.

我想要的东西:

  • 用户单击下载PDF"链接.

  • User clicks the "download PDF" link.

XFDF是即时生成的(没有文件写入服务器).

XFDF is generated on the fly (no file written to server).

PDF和XFDF使用通用PDF合并在服务器端.最终PDF的内容将输出到与XFDF原始用户相同的用户.

PDF and XFDF are merged server-side using generic PDF. Contents of final PDF are output to user same as XFDF was originally.

XFDF和最终PDF都不会保存到服务器.

Neither the XFDF nor the final PDF are ever saved to server.

我正在查看 pdftk ,其中有一些用于处理FDF/XFDF文件的选项,但是所有这些都假设a)原始XFDF文件作为服务器上的文件存在,并且b)生成的PDF应该作为文件在服务器上输出.

I was looking at pdftk, which has a few options for dealing with FDF/XFDF files, but all of which assume that a) the original XFDF file exists as a file on the server and b) that the resulting PDF should be output as a file on the server.

此外,pdftk已有4岁.

Also, pdftk is 4 years old.

我想知道是否:

a)那里有一个比pdftk更新的版本?

a) there was a newer equivalent to pdftk out there?

b)是否有办法使用更新的类似pdftk的工具或使用pdftk来使用动态路径,以使数据永远不必以文件形式存在于服务器上?

b) if there was a way, using a newer pdftk-like tool or using pdftk, to use dynamic paths so that the data never has to exist in file form on the server?

我最近使用php://了解了内置的输入/输出流,但是我仍然对如何使用它感到迷惑不解,但这也许是个好地方吗?

I recently learned about the built-in input/output streams using php:// but I'm still really fuzzy on how to use it, but maybe this would be a good place for this?

推荐答案

我本人接受了khkremer的建议,并用C#构建了一个小型.net exe文件,以便基于itextsharp库从xfdf文件创建PDF.仍然需要使用passthru来调用它,但是它很好而且很小.我的版本还可以选择将隐藏字段插入pdf,因为我的项目需要该功能.

I took khkremer's advice myself and built a small .net exe in C# to create a PDF from an xfdf file based on the itextsharp library. It will still need to be called using passthru, but it's nice and tiny. My version also has the option to insert a hidden field into a pdf since I needed that functionality for my project.

您将需要下载引用并将其添加到iTextSharp.text.pdf程序集.

You will need to download and add the reference to the iTextSharp.text.pdf assembly.

希望有帮助!

 using System;
 using System.IO;
 using iTextSharp.text.pdf;

namespace PDFBrain
{
    class Program
    {
        // args:
        // 0 => template
        // 1 => xfdf
        // 2 => outputfile
        // 3 => flatten output file?
        static void Main(string[] args)
        {
            if (args == null)
            {
                Console.Out.WriteLine("No arguments were provided. Exiting.");
                return;
            }

            if (args[0] == "create")
            {
                if (args.Length != 5)
                {
                    Console.Out.WriteLine("Wrong number of arguments were provided. Exiting.");
                    return;
                }
                CreatePDF(args[1], args[2], args[3], args[4]);    
            }

            if (args[0] == "hidden")
            {
                if (args.Length != 3)
                {
                    Console.Out.WriteLine("Wrong number of arguments were provided. Exiting.");
                    return;
                }

                InsertHiddenIdField(args[1], args[2]);
            }

        }


public static void CreatePDF(string templ, string xfdf, string output, string flatten)
    {
        PdfReader template = new PdfReader(templ);

        XfdfReader xfdfReader = new XfdfReader(xfdf);

        PdfStamper stamper = new PdfStamper(template, new FileStream(output, FileMode.Create));
        stamper.AcroFields.SetFields(xfdfReader);

        stamper.FormFlattening = flatten == "true" ? true : false;

        stamper.Writer.CloseStream = false;
        stamper.Close();

    }

    public static void InsertHiddenIdField(string templ, string output)
    {
        PdfReader template = new PdfReader(templ);

        PdfStamper stamper = new PdfStamper(template, new FileStream(output, FileMode.Create));

        TextField clientID = new TextField(stamper.Writer, new iTextSharp.text.Rectangle(10,750,60,770), "hdnClientID");
        clientID.Visibility = BaseField.HIDDEN;

        stamper.AddAnnotation(clientID.GetTextField(),1);

        stamper.Close();            
    }
}

}

这篇关于将XFDF与PDF表单合并以创建最终的PDF服务器端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆