Skip to content

Commit

Permalink
Charset decode on metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Jul 30, 2024
1 parent 8a9b844 commit c30f728
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/PDFDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,27 @@ public function set_tsa($tsa, $tsauser = null, $tsapass = null) {
* @param $contact
* @return void
*/
public function set_metadata_props($name = null, $reason = null, $location = null, $contact = null)
public function set_metadata_props($name = null, $reason = null, $location = null, $contact = null, $encoding = 'ISO-8859-1')
{
$this->_metadata_name = $name;
$this->_metadata_reason = $reason;
$this->_metadata_location = $location;
$this->_metadata_contact_info = $contact;
$this->_metadata_name = $this->charset_encode($name, $encoding);
$this->_metadata_reason = $this->charset_encode($reason, $encoding);
$this->_metadata_location = $this->charset_encode($location, $encoding);
$this->_metadata_contact_info = $this->charset_encode($contact, $encoding);
}

private function charset_encode($string, $to)
{
if (!is_string($string) || !function_exists('mb_detect_encoding')) {
return $string;
}

$encoding = mb_detect_encoding($string, mb_list_encodings(), true);

if ($encoding !== $to) {
$string = mb_convert_encoding($string, $to, $encoding);
}

return $string;
}

/**
Expand Down

0 comments on commit c30f728

Please sign in to comment.