Flatten FDF / XFDF forms to PDF in PHP with utf-8 characters

Mateng picture Mateng · Oct 20, 2010 · Viewed 12.9k times · Source

My scenario:

  • A PDF template with formfields: template.pdf
  • An XFDF file that contains the data to be filled in: fieldData.xfdf

Now I need to have these to files combined & flattened. pdftk does the job easily within php:

exec("pdftk template.pdf fill_form fieldData.xfdf output flatFile.pdf flatten");

Unfortunately this does not work with full utf-8 support. For example: Cyrillic and greek letters get scrambled. I used Arial for this, with an unicode character set.

  • How can I accomplish to flatten my unicode files?
  • Is there any other pdf tool that offers unicode support?
  • Does pdftk have an unicode switch that I am missing?

EDIT 1: As this question has not been solved for more then 9 month, I decided to start a bounty for it. In case there are options to sponsor a feature or a bugfix in pdftk, I'd be glad to donate.

EDIT 2: I am not working on this project anymore, so I cannot verify new answers. If anyone has a similar problem, I am glad if they can respond in my favour.

Answer

Matthew Cheale picture Matthew Cheale · Jan 23, 2013

I found by using Jon's template but using the DomDocument the numeric encoding was handled for me and worked well. My slight variation is below:

$xml = new DOMDocument( '1.0', 'UTF-8' );

$rootNode = $xml->createElement( 'xfdf' );
$rootNode->setAttribute( 'xmlns', 'http://ns.adobe.com/xfdf/' );
$rootNode->setAttribute( 'xml:space', 'preserve' );
$xml->appendChild( $rootNode );

$fieldsNode = $xml->createElement( 'fields' );
$rootNode->appendChild( $fieldsNode );

foreach ( $fields as $field => $value )
{
    $fieldNode = $xml->createElement( 'field' );
    $fieldNode->setAttribute( 'name', $field );
    $fieldsNode->appendChild( $fieldNode );

    $valueNode = $xml->createElement( 'value' );
    $valueNode->appendChild( $xml->createTextNode( $value ) );
    $fieldNode->appendChild( $valueNode );
}

$xml->save( $file );