Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ parameters:
-
message: '#^Variable \$js in isset\(\) always exists and is not nullable\.$#'
identifier: isset.variable
count: 2
count: 1
path: src/Form.php

-
Expand Down
115 changes: 91 additions & 24 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ class Form
var $formAction;
var $form_fonts;
var $form_radio_groups;

/**
* @var array[] by name, the push buttons that share it, which are written as one field with the widgets as kids
*/
private $buttonGroups = [];

var $form_checkboxes;
var $pdf_acro_array;
var $pdf_array_co;
Expand Down Expand Up @@ -688,12 +694,12 @@ private function getCountItems($form)
}

if ($form['typ'] === 'Bt') {
if (isset($this->array_form_button_js[$form['T']])) {
if (isset($this->array_form_button_js[$form['n']])) {
$total++;
}
if (isset($this->form_button_icon[$form['T']])) {
if (isset($this->form_button_icon[$form['n']])) {
$total++;
if ($this->form_button_icon[$form['T']]['Indexed']) {
if ($this->form_button_icon[$form['n']]['Indexed']) {
$total++;
}
}
Expand Down Expand Up @@ -729,6 +735,61 @@ function addFormIds($n, array &$annots, &$id)
}
}

/**
* Gathers the push buttons that share a name and numbers the field each group is written as, from $id. A button
* with a name of its own stays a single field and widget.
*
* @param int $id the first group's object number, left at the next free one
*/
function addButtonGroupIds(&$id)
{
$names = [];
foreach ($this->forms as $form) {
if ($this->isPushButton($form)) {
$names[$form['T']][] = $form['n'];
}
}

$this->buttonGroups = [];
foreach ($names as $name => $kids) {
if (count($kids) > 1) {
$this->buttonGroups[$name] = ['obj_id' => $id++, 'kids' => $kids];
}
}
}

/**
* Whether a field is a submit, reset, script or image button, which is pressed rather than switched on and off
*
* @param mixed[] $form
*
* @return bool
*/
private function isPushButton($form)
{
return $form['typ'] === 'Bt' && $form['subtype'] !== 'radio' && $form['subtype'] !== 'checkbox';
}

/**
* Writes each group of same-named push buttons as a field holding the name and the push-button flag, with the
* widgets as its kids. It is not an annotation, so no page lists it.
*/
function putButtonGroups()
{
foreach ($this->buttonGroups as $name => $group) {
$this->writer->object();
$this->pdf_acro_array .= $this->mpdf->n . ' 0 R ';

$kids = '';
foreach ($group['kids'] as $kid) {
$kids .= $this->forms[$kid]['obj'] . ' 0 R ';
}

$this->writer->write('<< /FT /Btn /Ff ' . $this->_setflag([17]) . ' /T ' . $this->writer->string($name) . ' /Kids [ ' . $kids . '] >>');
$this->writer->write('endobj');
}
}

// In _putannots
function _putFormItems($n, $hPt)
{
Expand Down Expand Up @@ -841,17 +902,20 @@ private function actionAllowed()
return false;
}

function SetFormButtonJS($name, $js)
/**
* Gives a button its script, by the button's number rather than its name, which other buttons may share
*
* @param int $n the button's number, its key in $forms
* @param string $js
*/
function SetFormButtonJS($n, $js)
{
if (!$this->actionAllowed()) {
return;
}
$js = str_replace("\t", ' ', trim($js));
if (isset($name) && isset($js)) {
$this->array_form_button_js[$this->writer->escape($name)] = [
'js' => $js
];
}
$this->array_form_button_js[$n] = [
'js' => str_replace("\t", ' ', trim($js))
];
}

function SetFormChoiceJS($name, $js)
Expand Down Expand Up @@ -1103,14 +1167,14 @@ function SetJSButton($w, $h, $name, $value, $js, $image_id = 0, $title = '', $fl
$this->SetFormButton($w, $h, $name, $value, 'js_button', $title, $flags, false, false, $background_col, $border_col, $noprint);
// pos => 1 = no caption, icon only; 0 = caption only
if ($image_id) {
$this->form_button_icon[$this->writer->escape($name)] = [
$this->form_button_icon[$this->formCount] = [
'pos' => 1,
'image_id' => $image_id,
'Indexed' => $indexed,
];
}
if ($js) {
$this->SetFormButtonJS($name, $js);
$this->SetFormButtonJS($this->formCount, $js);
}
$this->mpdf->x += $w;
}
Expand Down Expand Up @@ -1549,8 +1613,8 @@ private function writeAppearance($form)
}
}

if (isset($this->form_button_icon[$form['T']])) {
$s .= sprintf(' q %.3F 0 0 %.3F 0 0 cm /I%d Do Q', $width, $height, $this->form_button_icon[$form['T']]['image_id']);
if (isset($this->form_button_icon[$form['n']])) {
$s .= sprintf(' q %.3F 0 0 %.3F 0 0 cm /I%d Do Q', $width, $height, $this->form_button_icon[$form['n']]['image_id']);
} elseif ($form['AP']['lines']) {
$s .= sprintf(' /Tx BMC q %.3F %.3F %.3F %.3F re W n BT', $border, $border, $width - 2 * $border, $height - 2 * $border);
$s .= sprintf(' /F%d %.3F Tf %s', $this->mpdf->fonts[$form['style']['font']]['i'], $form['AP']['size'], $form['style']['fontcolor']);
Expand Down Expand Up @@ -1691,7 +1755,9 @@ function _putform_bt($form, $hPt)
$this->writer->object();
$n = $this->mpdf->n;

if ($form['subtype'] !== 'radio') {
// A radio button or a button that shares its name is listed through the field it is a kid of
$group = $this->isPushButton($form) && isset($this->buttonGroups[$form['T']]);
if ($form['subtype'] !== 'radio' && !$group) {
$this->pdf_acro_array .= $n . ' 0 R '; // Add to /Field element
}

Expand All @@ -1711,13 +1777,15 @@ function _putform_bt($form, $hPt)
$this->writer->write('/FT /Btn ');
$this->writer->write('/H /P ');

if ($form['subtype'] !== 'radio') { // mPDF 5.3.23
if ($group) {
$this->writer->write('/Parent ' . $this->buttonGroups[$form['T']]['obj_id'] . ' 0 R ');
} elseif ($form['subtype'] !== 'radio') { // mPDF 5.3.23
$this->writer->write('/T ' . $this->writer->string($form['T']));
}

$this->writer->write('/TU ' . $this->writer->string($form['TU']));

if (isset($this->form_button_icon[$form['T']])) {
if (isset($this->form_button_icon[$form['n']])) {
$form['BS_W'] = 0;
}

Expand Down Expand Up @@ -1849,14 +1917,14 @@ function _putform_bt($form, $hPt)

if ($form['subtype'] === 'js_button') {
// Icon / image
if (isset($this->form_button_icon[$form['T']])) {
if (isset($this->form_button_icon[$form['n']])) {
$cc++;
$temp .= '/TP ' . $this->form_button_icon[$form['T']]['pos'] . ' ';
$temp .= '/TP ' . $this->form_button_icon[$form['n']]['pos'] . ' ';
$temp .= '/I ' . ($cc + $this->mpdf->n) . ' 0 R '; // Normal icon
$temp .= '/RI ' . ($cc + $this->mpdf->n) . ' 0 R '; // onMouseOver
$temp .= '/IX ' . ($cc + $this->mpdf->n) . ' 0 R '; // onClick / onMouseDown
$temp .= '/IF << /SW /A /S /A /A [0.0 0.0] >> '; // Icon fit dictionary
if ($this->form_button_icon[$form['T']]['Indexed']) {
if ($this->form_button_icon[$form['n']]['Indexed']) {
$cc++;
}
$put_icon = 1;
Expand All @@ -1870,7 +1938,7 @@ function _putform_bt($form, $hPt)
$form['FF'][] = 17;
$this->writer->write('/Ff ' . $this->_setflag($form['FF']));
// Javascript
if (isset($this->array_form_button_js[$form['T']])) {
if (isset($this->array_form_button_js[$form['n']])) {
$cc++;
$this->writer->write('/AA << /D ' . ($cc + $this->mpdf->n) . ' 0 R >>');
$put_js = 1;
Expand All @@ -1884,13 +1952,12 @@ function _putform_bt($form, $hPt)
// additional objects
// obj icon
if ($put_icon === 1) {
$this->_put_button_icon($this->form_button_icon[$form['T']], $form['w'], $form['h']);
$this->_put_button_icon($this->form_button_icon[$form['n']], $form['w'], $form['h']);
$put_icon = null;
}
// obj + 1
if ($put_js === 1) {
$this->mpdf->_set_object_javascript($this->array_form_button_js[$form['T']]['js']);
unset($this->array_form_button_js[$form['T']]);
$this->mpdf->_set_object_javascript($this->array_form_button_js[$form['n']]['js']);
$put_js = null;
}

Expand Down
6 changes: 5 additions & 1 deletion src/Writer/MetadataWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,8 @@ public function writeAnnotations() // _putannots
$this->form->_putRadioItems($n);
}

$this->form->putButtonGroups();

$this->annotationFiles = [];
}

Expand All @@ -1025,7 +1027,7 @@ public function writeAnnotations() // _putannots
*
* Each page gets its links, then its annotations, then its form widgets. An annotation's ids name the annotation
* itself, its embedded file (under PDF/A-3 its file specification, then the file) or else its popup, and its
* appearance under PDF/A. The radio groups come last.
* appearance under PDF/A. The radio groups come last, then the groups of push buttons that share a name.
*
* @param int $id the object number after the last page's
*
Expand Down Expand Up @@ -1076,6 +1078,8 @@ public function numberAnnotations($id)
$this->form->form_radio_groups[$name]['obj_id'] = $id++;
}

$this->form->addButtonGroupIds($id);

return $annots;
}

Expand Down
40 changes: 30 additions & 10 deletions tests/Mpdf/AnnotationObjectNumbersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class AnnotationObjectNumbersTest extends \Yoast\PHPUnitPolyfills\TestCases\Test
use PageStreams;

/**
* Every /Annots entry is an annotation of its page, and every /Popup, /Parent, /EF, /AP and radio /Parent
* reference names an object of the kind it should
* Every /Annots entry is an annotation of its page, and every /Popup, /Parent, /EF, /AP and radio or button /Parent
* reference names an object of the kind it should. A field that gathers widgets is listed by /Fields, not /Annots.
*
* @dataProvider documents
*
Expand All @@ -30,11 +30,14 @@ public function testReferencesNameTheRightObjects($config, $forms, $expected)
$this->assertEveryObjectWritten($pdf);

$subtypes = [];
$listed = [];
$parents = [];
foreach ($this->pageObjects($pdf) as $i => $page) {
$refs = $this->annotsOf($pdf, $page);
$subtypes[$i] = [];

foreach ($refs as $ref) {
$listed[] = $ref;
$annot = $this->object($pdf, $ref);
$this->assertStringContainsString('/Type /Annot', $annot);
preg_match('/\/Subtype \/(\w+)/', $annot, $subtype);
Expand Down Expand Up @@ -72,11 +75,24 @@ public function testReferencesNameTheRightObjects($config, $forms, $expected)

if ($subtype[1] === 'Widget' && preg_match('/\/Parent (\d+) 0 R/', $annot, $group)) {
$this->assertMatchesRegularExpression('/\/Kids \[[^\]]*\b' . $ref . ' 0 R/', $this->object($pdf, $group[1]));
$this->assertStringNotContainsString('/T (', $annot, 'A kid takes its name from its field');
$parents[] = $group[1];
}
}
}

$this->assertSame($expected, $subtypes);

if ($forms) {
preg_match('/\/Fields \[([^\]]*)\]/', $pdf, $list);
preg_match_all('/(\d+) 0 R/', $list[1], $fields);
$parents = array_unique($parents);
$this->assertCount(4, $parents, 'Two radio groups and two groups of buttons');
foreach ($parents as $parent) {
$this->assertNotContains($parent, $listed, 'A field is not an annotation');
$this->assertContains($parent, $fields[1]);
}
}
}

/**
Expand All @@ -87,24 +103,25 @@ public function testReferencesNameTheRightObjects($config, $forms, $expected)
public function documents()
{
$notes = ['Link', 'Text', 'Popup', 'Text', 'Text', 'Text', 'Popup'];
$widgets = ['Widget', 'Widget', 'Widget', 'Widget', 'Widget'];
$widgets = ['Widget', 'Widget', 'Widget', 'Widget', 'Widget', 'Widget', 'Widget', 'Widget', 'Widget'];
$second = ['Link', 'Text', 'Popup'];

$withFile = ['Link', 'Text', 'Popup', 'FileAttachment', 'Text', 'Text', 'Popup'];

return [
'file not allowed, forms' => [['mode' => 'c', 'useActiveForms' => true], true, [array_merge($notes, $widgets), array_merge($second, ['Widget', 'Widget', 'Widget'])]],
'embedded fonts, forms' => [['useActiveForms' => true], true, [array_merge($notes, $widgets), array_merge($second, ['Widget', 'Widget', 'Widget'])]],
'file allowed, forms' => [['mode' => 'c', 'useActiveForms' => true, 'allowAnnotationFiles' => true, 'allowHtmlAnnotationFiles' => true], true, [array_merge($withFile, $widgets), array_merge($second, ['Widget', 'Widget', 'Widget'])]],
'file not allowed, forms' => [['mode' => 'c', 'useActiveForms' => true], true, [array_merge($notes, $widgets), array_merge($second, ['Widget', 'Widget', 'Widget', 'Widget', 'Widget'])]],
'embedded fonts, forms' => [['useActiveForms' => true], true, [array_merge($notes, $widgets), array_merge($second, ['Widget', 'Widget', 'Widget', 'Widget', 'Widget'])]],
'file allowed, forms' => [['mode' => 'c', 'useActiveForms' => true, 'allowAnnotationFiles' => true, 'allowHtmlAnnotationFiles' => true], true, [array_merge($withFile, $widgets), array_merge($second, ['Widget', 'Widget', 'Widget', 'Widget', 'Widget'])]],
'PDF/A-2 appearances, file not a PDF/A' => [['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => '2-B', 'allowAnnotationFiles' => true, 'allowHtmlAnnotationFiles' => true], false, [$notes, $second]],
'PDF/A-2 appearances, forms' => [['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => '2-B', 'useActiveForms' => true], true, [array_merge($notes, $widgets), array_merge($second, ['Widget', 'Widget', 'Widget'])]],
'PDF/A-2 appearances, forms' => [['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => '2-B', 'useActiveForms' => true], true, [array_merge($notes, $widgets), array_merge($second, ['Widget', 'Widget', 'Widget', 'Widget', 'Widget'])]],
'PDF/A-3 appearances and file' => [['PDFA' => true, 'PDFAauto' => true, 'PDFAversion' => '3-B', 'allowAnnotationFiles' => true, 'allowHtmlAnnotationFiles' => true], false, [$withFile, $second]],
];
}

/**
* Two pages of links, notes with and without popups, a note with a file and, when asked, form widgets with a
* radio group on each page
* radio group on each page, two buttons named go on the first and one on the second, and buttons named b and
* lone
*
* @param bool $forms
*
Expand All @@ -118,13 +135,16 @@ private function html($forms)
if ($forms) {
$html .= '<form><input type="text" name="t1" value="x" onchange="alert(1)" /> <input type="checkbox" name="c1" value="1" checked="checked" />'
. ' <input type="radio" name="r1" value="a" checked="checked" /> <input type="radio" name="r1" value="b" />'
. ' <select name="s1" onchange="x()"><option value="1">1</option></select></form>';
. ' <select name="s1" onchange="x()"><option value="1">1</option></select>'
. ' <input type="button" name="go" value="A" onclick="a()" /> <input type="submit" name="go" value="B" />'
. ' <input type="image" name="b" src="' . __DIR__ . '/../data/img/ratio-16x9.png" onclick="b()" /> <input type="reset" name="lone" /></form>';
}

$html .= '<pagebreak /><p id="b"><a href="https://example.com">ext</a> <annotation content="Second" popup="true" /></p>';

if ($forms) {
$html .= '<form><input type="radio" name="r2" value="c" /> <input type="radio" name="r2" value="d" checked="checked" /> <input type="text" name="t2" /></form>';
$html .= '<form><input type="radio" name="r2" value="c" /> <input type="radio" name="r2" value="d" checked="checked" /> <input type="text" name="t2" />'
. ' <input type="button" name="go" value="C" onclick="c()" /> <input type="image" name="b" src="' . __DIR__ . '/../data/img/ratio-9x16.png" /></form>';
}

return $html;
Expand Down
6 changes: 4 additions & 2 deletions tests/Mpdf/PdfaConformanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ public function testVisibilityConforms($version)

/**
* An active form conforms: checkboxes and radio buttons are drawn without ZapfDingbats, every widget carries its
* appearance, and the JavaScript, submit and reset actions PDF/A forbids are left out (#348)
* appearance, and the JavaScript, submit and reset actions PDF/A forbids are left out (#348). Two of the buttons
* share a name and are written as kids of one field (#443).
*
* @dataProvider documents
*/
Expand All @@ -147,7 +148,8 @@ public function testActiveFormConforms($version, $config)
. '<p><select name="s" onchange="app.alert(2)"><option value="1">One</option><option value="2" selected="selected">Two</option></select>'
. ' <select name="m" size="3" multiple="multiple"><option value="1">One</option><option value="2" selected="selected">Two</option></select></p>'
. '<p><input type="submit" name="go" value="Send" /> <input type="reset" name="rs" value="Reset" />'
. ' <input type="button" name="b" value="Push" onclick="app.alert(3)" noprint="noprint" /></p>'
. ' <input type="button" name="b" value="Push" onclick="app.alert(3)" noprint="noprint" />'
. ' <input type="button" name="b" value="Again" onclick="app.alert(4)" /></p>'
. '</form>'
);

Expand Down
Loading
Loading