英文:
I need to programmatically fill out a PDF form's radio buttons, can I do this in JS, go, or python?
问题
我发现了一个用于Node.js的pdf-fill-form库,并且成功地用它来填写一个PDF申请表,以便与用户的在线申请匹配。唯一的问题是,PDF中有几个单选按钮,比如性别,但是当我尝试将字段"Sex"的值设置为"Male"或"Female"时,没有任何响应。
根据pdf-fill-form的文档,它明确表示只支持文本和复选框,所以我理解这一点,但是我找不到除了Java或C#/C++之外的其他解决方案,而我真的不想为了解决这个看起来相当常见的问题而添加另一种技术。有人知道其他的解决方案吗?或者有没有其他的解决办法?谢谢。
英文:
I found pdf-fill-form for node.js and am using that well for filling out a PDF application that a client needs to match the user's online application. The only issue is that the pdf has a few radio buttons, like for gender, and I get no response when I try and set the field "Sex" value to "Male" or "Female" even though those are the values.
var vals = {"Sex": "Male"}
pdfFillForm.write(sourcePDF, vals, { "save": "pdf" } )
.then(function(result) {
fs.writeFile(destinationPDF, result, function(err){
if (err) {
return console.log(err);
}
console.log("Success Saving");
});
}, function(err) {
console.log(err);
});
It says clearly in pdf-fill-form documentation that it only supports text and checkboxes so I get it, but I can't find anything outside of Java or C#/C++ that can do this, and I really don't want to add another technology to solve what seems like a fairly common problem. Anyone know of anything? Or is there a work around? Thanks.
答案1
得分: 1
我自己修复了这个问题,实际上并不难。我只是更新了pdf-fill-form代码,并正在等待关于我的拉取请求的回复。但如果有人在此期间需要它,可以访问https://github.com/mttchrry/pdf-fill-form。
英文:
I made the fix myself, actually wasn't to bad. I just updated the pdf-fill-form code, and am waiting to hear back about my pull request, but if anyone needs it in the meantime, https://github.com/mttchrry/pdf-fill-form
答案2
得分: 0
使用pypdftk填充PDF的解决方案
输入PDF文件
PDF文件是在LibreOffice Draw
中创建的,使用文件/导出为/导出为PDF..选项。
一个组中的所有单选按钮都具有相同的名称,没有组名称。
(如果设置不同的名称和相同的组名称,似乎会得到相同的结果)
在安装了pdftk
的计算机上,对包含至少两个单选按钮的输入PDF文件运行以下命令:
pdftk in.pdf dump_data_fields > fields.list
文件fields.list应该有一个类似于以下的部分:
FieldType: Button
FieldName: radio_button_cooking
FieldFlags: 49152
FieldValue: 1
FieldValueDefault: Off
FieldJustification: Left
FieldStateOption: 1
FieldStateOption: 2
FieldStateOption: 3
FieldStateOption: Off
这意味着该组中有两个按钮,名称相同为radio_button_cooking。
如果组中有更多按钮,值将继续为4,..
使用pypdftk填充PDF
按钮的可能值为1、2或3,或者没有选择。
import pypdftk
val_rare = 1 #猜测将值与选项关联起来
val_medium = 2
val_welldone = 3
fields = {
'my_radio_button': val_medium,
}
pypdftk.fill_form("in.pdf", fields, "out.pdf", flatten=False)
现在,文件out.pdf应该选择了两个单选按钮中的一个。
您需要进行一些猜测,以确定哪个按钮具有值1、2和3。
如果您不想使用pypdftk
,还有其他选项。
英文:
My solution for python using pypdftk
Input PDF file
The pdf file is created in LibreOffice Draw
, using the File/Export As/Export as PDF..
The radio buttons of a group all have the same name, and no group name.
(It seems you get the same result if setting different names and the same group name)
On a machine having pdftk
installed, run the following on your input pdf file containing at least two radio buttons in a group:
pdftk in.pdf dump_data_fields > fields.list
File fields.list should have a section like:
FieldType: Button
FieldName: radio_button_cooking
FieldFlags: 49152
FieldValue: 1
FieldValueDefault: Off
FieldJustification: Left
FieldStateOption: 1
FieldStateOption: 2
FieldStateOption: 3
FieldStateOption: Off
Meaning there are two buttons in the group, with the same name radio_button_cooking.
If you have more buttons in the group, the values continue with 4, ..
Fill the PDF using pypdftk
Possible values for the buttons are here either 1, 3, or 3. Or nothing for no selection.
import pypdftk
val_rare = 1 #guesswork to link values to options
val_medium = 2
val_welldone = 3
fields = {
'my_radio_button': val_medium,
}
pypdftk.fill_form("in.pdf", fields, "out.pdf", flatten=False)
File out.pdf should now have one of both radio buttons selected.
You have to do some guesswork to find out which button has value 1, 2 and 3.
If you don't want to use pypdftk
, there are other options.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论