ZendPDF修改元数据XML


PHP ZendPDF Modify Metadata XML

我试图在Symfony中使用ZendPDF创建PDF/a -1b

要做到这一点,我必须在PDF的XML结构中设置一些元数据。但我不知道该怎么做。

我试图修改XML通过DOMDocument但是当我试图添加一个DOMNode它说它不能写属性

$node = new DOMNode();
$node->nodeName = "part";

然后我得到Cannot write property Exception

我只需要在DOMDocument上附加一个子元素

完整代码

$metadata = $this->pdf->getMetadata();
    $metadataDOM = new DOMDocument();
    $metadataDOM->loadXML($metadata);
    $xpath = new DOMXPath($metadataDOM);
    $xpath->registerNamespace('x', 'adobe:ns:meta/');
    $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
    $xpath->registerNamespace('xmp', 'http://ns.adobe.com/xap/1.0/');
    $xpath->registerNamespace('xmpGImg', 'http://ns.adobe.com/xap/1.0/g/img/');
    $xpath->registerNamespace('xmpTPg', 'http://ns.adobe.com/xap/1.0/t/pg/');
    $xpath->registerNamespace('stDim', 'http://ns.adobe.com/xap/1.0/sType/Dimensions#');
    $xpath->registerNamespace('xmpG', 'http://ns.adobe.com/xap/1.0/g/');
    $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
    $xpath->registerNamespace('xmpMM', 'http://ns.adobe.com/xap/1.0/mm/');
    $xpath->registerNamespace('pdf', 'http://ns.adobe.com/pdf/1.3/');
    $xpath->registerNamespace('pdfaid', 'http://www.aiim.org/pdfa/ns/id');
    $data = $xpath->query('//rdf:Description')->item(0);
    $node = new DOMNode();
    $node->nodeName = "asdf";
    $data->appendChild($node);
    dump($data);
    dump($metadataDOM->saveXML());
    die;

您的问题是nodeName属性是只读的参见:http://php.net/manual/en/class.domnode.php

定义在domNode类中:public readonly string $nodeName;

在下面的链接中,还有一个解决方案是使用其他只读元素"textContent"

http://php.net/manual/en/class.domnode.php # 95545

David Rekowski从php.net获得的解决方案副本:

您不能简单地覆盖$textContent来替换文本内容正如缺失的只读标志所表明的那样。相反,你有执行如下操作:

<?php
$node->removeChild($node->firstChild);
$node->appendChild(new DOMText('new text content'));
?>
This example shows what happens:
<?php
$doc = DOMDocument::loadXML('<node>old content</node>');
$node = $doc->getElementsByTagName('node')->item(0);
echo "Content 1: ".$node->textContent."'n";
$node->textContent = 'new content';
echo "Content 2: ".$node->textContent."'n";
$newText = new DOMText('new content');
$node->appendChild($newText);
echo "Content 3: ".$node->textContent."'n";
$node->removeChild($node->firstChild);
$node->appendChild($newText);
echo "Content 4: ".$node->textContent."'n";
?>
The output is:
Content 1: old content // starting content
Content 2: old content // trying to replace overwriting $node->textContent
Content 3: old contentnew content // simply appending the new text node
Content 4: new content // removing firstchild before appending the new text node
If you want to have a CDATA section, use this:
<?php
$doc = DOMDocument::loadXML('<node>old content</node>');
$node = $doc->getElementsByTagName('node')->item(0);
$node->removeChild($node->firstChild);
$newText = $doc->createCDATASection('new cdata content');
$node->appendChild($newText);
echo "Content withCDATA: ".$doc->saveXML($node)."'n";
?>

顺便说一句。这个问题和symfony一点关系都没有

问题解决

我创建了基本PDF PDF/A-1b符合,不需要修改元数据了