Skip to content

Instantly share code, notes, and snippets.

@Gergling
Last active April 9, 2018 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gergling/c5ab7976275e0240c2235a091ea05228 to your computer and use it in GitHub Desktop.
Save Gergling/c5ab7976275e0240c2235a091ea05228 to your computer and use it in GitHub Desktop.
Takes a `pdftk` field dump and turns it into a PHP-copyable output.
<?php
// Usage example: pdftk stuff.pdf dump_data_fields | php pdf-fields.php
$stdin = file('php://stdin');
// print_r($stdin);
$data = [];
$i = 0;
foreach($stdin as $idx => $line) {
$chunks = explode(': ', $line);
$name = trim($chunks[0]);
if ($name === '---') {
$i += 1;
}
if (isset($chunks[1])) {
$value = trim($chunks[1]);
if ($name === 'FieldStateOption') {
$data[$i][$name][] = $value;
} else {
$data[$i][$name] = $value;
}
}
}
foreach($data as $i => $field) {
$fieldName = $field['FieldName'];
$options = '';
if ($field['FieldType'] === 'Button') {
$options = ' // ' . implode(', ', $field['FieldStateOption']);
}
echo "'$fieldName' => '$i $fieldName',$options\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment