PHP ZendPDF修改元数据XML

时间:2015-10-14 11:27:57

标签: php xml symfony pdf

我尝试使用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;

2 个答案:

答案 0 :(得分:0)

您的问题是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的David Rekowski的解决方案副本:

  

您不能简单地覆盖$ 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无关。

答案 1 :(得分:0)

解决问题

我创建了基础PDF PDF / A-1b,并且不再需要修改元数据