我正在使用ZendPDF在Symfony中创建一个PDF/A-1b
为此,我必须在PDF的XML结构中设置一些元数据。但我不知道该怎么做。
我试图通过DOMDocument修改XML,但是当我尝试添加DOMNode时,它说它不能写入属性
$node = new DOMNode();
$node->nodeName = "part";但是我不能写属性异常
我只需要在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;发布于 2015-10-14 12:19:23
您的问题是nodeName属性是只读的--请参阅:http://php.net/manual/en/class.domnode.php
在这里定义在domNode类中:public readonly string $nodeName;
在下面的链接中,还有一个其他元素的解决方案,即只读"textContent“。
http://php.net/manual/en/class.domnode.php#95545
来自php.net的的解决方案的副本:
您不能像缺少的只读标志所建议的那样,简单地覆盖$textContent来替换DOMNode的文本内容。相反,你必须这样做:
<?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毫无关系。
发布于 2015-10-19 12:42:04
问题解决
我创建了基本的PDF/A-1b格式,不再需要修改元数据了。
https://stackoverflow.com/questions/33124112
复制相似问题