Reputation: 2215
I tryed to build a FDF-File in php but i have problems with "<<". It's a bit strange the "<<" everything was before the string is deleted. I know the character is an operator but how can I use it to construct a string now?
here is my code:
$fdf = '%FDF-1.2
1 0 obj<</FDF<< /Fields[';
$fields = "";
foreach($dataContainer as $data)
{
if(array_key_exists($data['fieldname'], $this->fieldSet))
{
$field = '('.$data['fieldname'].')';
$value = '('.$data['content'].')';
$fields.='<</T'.$field.'/V'.$value.'>>';
echo $fields." ".$data['fieldname']."<br />";
}
}
$fdf.= $fields;
$fdf.= '] >> >>
endobj
trailer
<</Root 1 0 R>>
%%EOF';
and the output is:
<
<>
<><>
<><><>
<><><><>
Has anyone a solution for this?
Upvotes: 0
Views: 49
Reputation: 72226
Your PDF is fine, there is nothing wrong with <<
or anything else in the code. The only problem is that you probably dump the generated PDF content directly and, because nobody tells it otherwise, the browser tries to interpret it as HTML.
The solution is very simple: use function header()
before outputting the PDF to tell the browser the correct type of the content you send:
header('Content-Type: application/pdf');
// ... Generate the PDF here
// ... and output it
echo($fdf);
That's all. Now, the browser knows the body of the response is not HTML and it won't try to display it, except if it knows how to read PDF (which the latest versions of Firefox and Chrome apparently do).
Depending on the browser and its settings, it will either display the PDF content rendered (as it would appear on the paper on printing) or it will open the default PDF viewer installed on the computer (Adobe Acrobat Reader, f.e.) or will ask where to save it or it will ask what of the above to do.
Upvotes: 1
Reputation: 7661
Adding htmlentities()
around strings with <, >
characters should fix your problem:
$fdf = '%FDF-1.2
1 0 obj<</FDF<< /Fields[';
$fields = "";
foreach($dataContainer as $data)
{
if(array_key_exists($data['fieldname'], $this->fieldSet))
{
$field = '('.$data['fieldname'].')';
$value = '('.$data['content'].')';
$fields.='<</T'.$field.'/V'.$value.'>>';
echo $fields." ".$data['fieldname']."<br />";
}
}
$fdf.= $fields;
$fdf.= '] >> >>
endobj
trailer
<</Root 1 0 R>>
%%EOF';
$fdf = htmlentities( $fdf ); // Adding it here would make sure every <, > is replaced
Upvotes: 0