PHP:从 PDF 中提取 fdf 字段作为数组

发布于 2024-12-26 05:32:04 字数 164 浏览 2 评论 0原文

我想从可填写的 pdf 中提取可用字段作为数组。

像这样的数组: array('firstname','secondname','address');

如果这些字段已填充,我不需要这些字段的值。

使用 PHP 实现这一点最简单的方法是什么?

i want to extract the available fields as an array from a fillable pdf.

an array like: array('firstname','secondname','address');

i do not need the values for those fields, if they are filled.

what is easiest way to do that using PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魂牵梦绕锁你心扉 2025-01-02 05:32:04

在“fdf_next_field_name”的在线文档下,给出了以下示例,您可以修改该示例以将字段名称存储到数组中

<?php
$fdf = fdf_open($HTTP_FDF_DATA);
for ($field = fdf_next_field_name($fdf); $field != ""; $field = fdf_next_field_name($fdf, $field)) {
    echo "field: $field\n";
}
?>

under online documentation for "fdf_next_field_name" the following example is given that you can modify to store the field names into an array

<?php
$fdf = fdf_open($HTTP_FDF_DATA);
for ($field = fdf_next_field_name($fdf); $field != ""; $field = fdf_next_field_name($fdf, $field)) {
    echo "field: $field\n";
}
?>
梦毁影碎の 2025-01-02 05:32:04

我赞成穆雷的回答,因为她在欧内斯特,我很确定他在 php 5.3 之前是正确的,

可悲的是,pecl fdf 已经不复存在了。

值得庆幸的是,一个“noah”对 php 文档发表了评论 preg_match_all 正则表达式解决该问题。为清楚起见,此处包含一些稍作修改的内容。诺亚万岁。

function parse($text_from_file) {
            if (!preg_match_all("/<<\s*\/V([^>]*)>>/x",$text_from_file,$out,PREG_SET_ORDER))
                    return;
            for ($i=0;$i<count($out);$i++) {
                    $pattern = "<<.*/V\s*(.*)\s*/T\s*(.*)\s*>>";
                    $thing = $out[$i][2];
                    if (eregi($pattern,$out[$i][0],$regs)) {
                            $key = $regs[2];
                            $val = $regs[1];
                            $key = preg_replace("/^\s*\(/","",$key);
                            $key = preg_replace("/\)$/","",$key);
                            $key = preg_replace("/\\\/","",$key);
                            $val = preg_replace("/^\s*\(/","",$val);
                            $val = preg_replace("/\)$/","",$val);
                            $matches[$key] = $val;
                    }
            }
            return $matches;
    }

我希望有人会厌倦 php 中缺乏真正的 fdf 支持并修复此问题。

由于如果您正在阅读这个问题,我们可能都遵循相同的基本工作流程,那么您应该知道我遵循的基本工作流程是:

HTH

-FT

I upvoted Murray's answer because her was in ernest and I am pretty sure that he is right pre php 5.3

Sadly, pecl fdf is no more.

Thankfully, one "noah" made a comment on the php documentation with a preg_match_all regex solution to the problem. Included here with slight modifications for clarity. Long live noah.

function parse($text_from_file) {
            if (!preg_match_all("/<<\s*\/V([^>]*)>>/x",$text_from_file,$out,PREG_SET_ORDER))
                    return;
            for ($i=0;$i<count($out);$i++) {
                    $pattern = "<<.*/V\s*(.*)\s*/T\s*(.*)\s*>>";
                    $thing = $out[$i][2];
                    if (eregi($pattern,$out[$i][0],$regs)) {
                            $key = $regs[2];
                            $val = $regs[1];
                            $key = preg_replace("/^\s*\(/","",$key);
                            $key = preg_replace("/\)$/","",$key);
                            $key = preg_replace("/\\\/","",$key);
                            $val = preg_replace("/^\s*\(/","",$val);
                            $val = preg_replace("/\)$/","",$val);
                            $matches[$key] = $val;
                    }
            }
            return $matches;
    }

I expect that someone will get fedup with the lack of true fdf support in php and fix this.

Since we are all probably after the same basic workflow if you are reading this question, then you should know that the basic workflow that I am following is:

HTH

-FT

趁年轻赶紧闹 2025-01-02 05:32:04

如果您控制 pdf 并且只需要密钥,则以下操作将起作用。使用 php,没有其他库(如果您的主机没有它们则很好)。

将 pdf 提交按钮设置为 html,并将页面设置为运行 php 代码的地址。
输入图片这里的描述

$q_string  = file_get_contents("php://input");
parse_str($q_string , $pdf_array);
$pdfkeys = array_keys($pdf_array);

来自 pdf 文件的 html 查询字符串被放入变量 $q_string 中。然后它被解析为一个名为 $pdf_array 的数组。 $pdf_array 保存所有键和值。然后使用 array_keys() 将所有键放入 $pdfkeys 中。

我来到这里寻找如何读取 pdf 值并将其放入数据库中,最后经过更多研究后提出了上述内容。希望能满足一些人的需求。 xfdf 也可以工作,但是你需要解析为 xml ——这对我来说更简单。

If you control the pdf and just want the keys, the following will work. Uses php, no other libraries (good if you host doesn't have them).

Set the pdf submit button to html and set the page to the address where your php code will run.
enter image description here

$q_string  = file_get_contents("php://input");
parse_str($q_string , $pdf_array);
$pdfkeys = array_keys($pdf_array);

The html query string, from the pdf file, is put into the variable $q_string. It is then parsed into an array called $pdf_array. $pdf_array holds all of the keys and values. Then array_keys() is used to put all the keys into $pdfkeys as you wanted.

I had come here looking how to read pdf values to put into a db, and finally after some more poking around came up with the above. Hopefully meets some people's needs. xfdf can also work, but you will need to parse as xml then -- this was simpler for me.

花心好男孩 2025-01-02 05:32:04

我从提交到我的服务器的 PDF 中收到正常的帖子,但不在 $_POST 数组中。你只需要从 php://input 解析它:

$allVars = file_get_contents("php://input");

parse_str($allVars, $myPost);

foreach($myPost as $key => $value) {
 $allKeys[] = $key;
}

I get a normal post from PDFs submitting to my server, but not in the $_POST array. You just have to parse it from php://input:

$allVars = file_get_contents("php://input");

parse_str($allVars, $myPost);

foreach($myPost as $key => $value) {
 $allKeys[] = $key;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文