/** * Writes a PDF value to the resulting document. * * Prepares the value for encryption of imported data by FPDI * * @param array $value */ protected function _prepareValue(&$value) { switch ($value[0]) { case pdf_parser::TYPE_STRING: if ($this->encrypted) { $value[1] = $this->_unescape($value[1]); $value[1] = $this->_encrypt_data($this->_currentObjId, $value[1]); $value[1] = TCPDF_STATIC::_escape($value[1]); } break; case pdf_parser::TYPE_STREAM: if ($this->encrypted) { $value[2][1] = $this->_encrypt_data($this->_currentObjId, $value[2][1]); $value[1][1]['/Length'] = array(pdf_parser::TYPE_NUMERIC, strlen($value[2][1])); } break; case pdf_parser::TYPE_HEX: if ($this->encrypted) { $value[1] = $this->hex2str($value[1]); $value[1] = $this->_encrypt_data($this->_currentObjId, $value[1]); // remake hexstring of encrypted string $value[1] = $this->str2hex($value[1]); } break; } }
/** * Encryption of imported data by FPDI * * @param array $value */ function pdf_write_value(&$value) { switch ($value[0]) { case PDF_TYPE_STRING: if ($this->encrypted) { $value[1] = $this->_unescape($value[1]); $value[1] = $this->_encrypt_data($this->_current_obj_id, $value[1]); $value[1] = TCPDF_STATIC::_escape($value[1]); } break; case PDF_TYPE_STREAM: if ($this->encrypted) { $value[2][1] = $this->_encrypt_data($this->_current_obj_id, $value[2][1]); $value[1][1]['/Length'] = array(PDF_TYPE_NUMERIC, strlen($value[2][1])); } break; case PDF_TYPE_HEX: if ($this->encrypted) { $value[1] = $this->hex2str($value[1]); $value[1] = $this->_encrypt_data($this->_current_obj_id, $value[1]); // remake hexstring of encrypted string $value[1] = $this->str2hex($value[1]); } break; } }
/** * Put encryption on PDF document. * @protected * @author Nicola Asuni * @since 2.0.000 (2008-01-02) */ protected function _putencryption() { if (!$this->encrypted) { return; } $this->encryptdata['objid'] = $this->_newobj(); $out = '<<'; if (!isset($this->encryptdata['Filter']) or empty($this->encryptdata['Filter'])) { $this->encryptdata['Filter'] = 'Standard'; } $out .= ' /Filter /' . $this->encryptdata['Filter']; if (isset($this->encryptdata['SubFilter']) and !empty($this->encryptdata['SubFilter'])) { $out .= ' /SubFilter /' . $this->encryptdata['SubFilter']; } if (!isset($this->encryptdata['V']) or empty($this->encryptdata['V'])) { $this->encryptdata['V'] = 1; } // V is a code specifying the algorithm to be used in encrypting and decrypting the document $out .= ' /V ' . $this->encryptdata['V']; if (isset($this->encryptdata['Length']) and !empty($this->encryptdata['Length'])) { // The length of the encryption key, in bits. The value shall be a multiple of 8, in the range 40 to 256 $out .= ' /Length ' . $this->encryptdata['Length']; } else { $out .= ' /Length 40'; } if ($this->encryptdata['V'] >= 4) { if (!isset($this->encryptdata['StmF']) or empty($this->encryptdata['StmF'])) { $this->encryptdata['StmF'] = 'Identity'; } if (!isset($this->encryptdata['StrF']) or empty($this->encryptdata['StrF'])) { // The name of the crypt filter that shall be used when decrypting all strings in the document. $this->encryptdata['StrF'] = 'Identity'; } // A dictionary whose keys shall be crypt filter names and whose values shall be the corresponding crypt filter dictionaries. if (isset($this->encryptdata['CF']) and !empty($this->encryptdata['CF'])) { $out .= ' /CF <<'; $out .= ' /' . $this->encryptdata['StmF'] . ' <<'; $out .= ' /Type /CryptFilter'; if (isset($this->encryptdata['CF']['CFM']) and !empty($this->encryptdata['CF']['CFM'])) { // The method used $out .= ' /CFM /' . $this->encryptdata['CF']['CFM']; if ($this->encryptdata['pubkey']) { $out .= ' /Recipients ['; foreach ($this->encryptdata['Recipients'] as $rec) { $out .= ' <' . $rec . '>'; } $out .= ' ]'; if (isset($this->encryptdata['CF']['EncryptMetadata']) and !$this->encryptdata['CF']['EncryptMetadata']) { $out .= ' /EncryptMetadata false'; } else { $out .= ' /EncryptMetadata true'; } } } else { $out .= ' /CFM /None'; } if (isset($this->encryptdata['CF']['AuthEvent']) and !empty($this->encryptdata['CF']['AuthEvent'])) { // The event to be used to trigger the authorization that is required to access encryption keys used by this filter. $out .= ' /AuthEvent /' . $this->encryptdata['CF']['AuthEvent']; } else { $out .= ' /AuthEvent /DocOpen'; } if (isset($this->encryptdata['CF']['Length']) and !empty($this->encryptdata['CF']['Length'])) { // The bit length of the encryption key. $out .= ' /Length ' . $this->encryptdata['CF']['Length']; } $out .= ' >> >>'; } // The name of the crypt filter that shall be used by default when decrypting streams. $out .= ' /StmF /' . $this->encryptdata['StmF']; // The name of the crypt filter that shall be used when decrypting all strings in the document. $out .= ' /StrF /' . $this->encryptdata['StrF']; if (isset($this->encryptdata['EFF']) and !empty($this->encryptdata['EFF'])) { // The name of the crypt filter that shall be used when encrypting embedded file streams that do not have their own crypt filter specifier. $out .= ' /EFF /' . $this->encryptdata['']; } } // Additional encryption dictionary entries for the standard security handler if ($this->encryptdata['pubkey']) { if ($this->encryptdata['V'] < 4 and isset($this->encryptdata['Recipients']) and !empty($this->encryptdata['Recipients'])) { $out .= ' /Recipients ['; foreach ($this->encryptdata['Recipients'] as $rec) { $out .= ' <' . $rec . '>'; } $out .= ' ]'; } } else { $out .= ' /R'; if ($this->encryptdata['V'] == 5) { // AES-256 $out .= ' 5'; $out .= ' /OE (' . TCPDF_STATIC::_escape($this->encryptdata['OE']) . ')'; $out .= ' /UE (' . TCPDF_STATIC::_escape($this->encryptdata['UE']) . ')'; $out .= ' /Perms (' . TCPDF_STATIC::_escape($this->encryptdata['perms']) . ')'; } elseif ($this->encryptdata['V'] == 4) { // AES-128 $out .= ' 4'; } elseif ($this->encryptdata['V'] < 2) { // RC-40 $out .= ' 2'; } else { // RC-128 $out .= ' 3'; } $out .= ' /O (' . TCPDF_STATIC::_escape($this->encryptdata['O']) . ')'; $out .= ' /U (' . TCPDF_STATIC::_escape($this->encryptdata['U']) . ')'; $out .= ' /P ' . $this->encryptdata['P']; if (isset($this->encryptdata['EncryptMetadata']) and !$this->encryptdata['EncryptMetadata']) { $out .= ' /EncryptMetadata false'; } else { $out .= ' /EncryptMetadata true'; } } $out .= ' >>'; $out .= "\n" . 'endobj'; $this->_out($out); }
/** * Returns the PDF string code to print a cell (rectangular area) with optional borders, background color and character string. The upper-left corner of the cell corresponds to the current position. The text can be aligned or centered. After the call, the current position moves to the right or to the next line. It is possible to put a link on the text.<br /> * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting. * @param $w (float) Cell width. If 0, the cell extends up to the right margin. * @param $h (float) Cell height. Default value: 0. * @param $txt (string) String to print. Default value: empty string. * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul> or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0))) * @param $ln (int) Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right (or left for RTL languages)</li><li>1: to the beginning of the next line</li><li>2: below</li></ul>Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0. * @param $align (string) Allows to center or align the text. Possible values are:<ul><li>L or empty string: left align (default value)</li><li>C: center</li><li>R: right align</li><li>J: justify</li></ul> * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false). * @param $link (mixed) URL or identifier returned by AddLink(). * @param $stretch (int) font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value. * @param $calign (string) cell vertical alignment relative to the specified Y value. Possible values are:<ul><li>T : cell top</li><li>C : center</li><li>B : cell bottom</li><li>A : font top</li><li>L : font baseline</li><li>D : font bottom</li></ul> * @param $valign (string) text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>M : middle</li><li>B : bottom</li></ul> * @return string containing cell code * @protected * @since 1.0 * @see Cell() */ protected function getCellCode($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = false, $link = '', $stretch = 0, $ignore_min_height = false, $calign = 'T', $valign = 'M') { // replace 'NO-BREAK SPACE' (U+00A0) character with a simple space $txt = str_replace(TCPDF_FONTS::unichr(160, $this->isunicode), ' ', $txt); $prev_cell_margin = $this->cell_margin; $prev_cell_padding = $this->cell_padding; $txt = TCPDF_STATIC::removeSHY($txt, $this->isunicode); $rs = ''; //string to be returned $this->adjustCellPadding($border); if (!$ignore_min_height) { $min_cell_height = $this->getCellHeight($this->FontSize); if ($h < $min_cell_height) { $h = $min_cell_height; } } $k = $this->k; // check page for no-write regions and adapt page margins if necessary list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y); if ($this->rtl) { $x = $this->x - $this->cell_margin['R']; } else { $x = $this->x + $this->cell_margin['L']; } $y = $this->y + $this->cell_margin['T']; $prev_font_stretching = $this->font_stretching; $prev_font_spacing = $this->font_spacing; // cell vertical alignment switch ($calign) { case 'A': // font top switch ($valign) { case 'T': // top $y -= $this->cell_padding['T']; break; case 'B': // bottom $y -= $h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent; break; default: case 'C': case 'M': // center $y -= ($h - $this->FontAscent - $this->FontDescent) / 2; break; } break; case 'L': // font baseline switch ($valign) { case 'T': // top $y -= $this->cell_padding['T'] + $this->FontAscent; break; case 'B': // bottom $y -= $h - $this->cell_padding['B'] - $this->FontDescent; break; default: case 'C': case 'M': // center $y -= ($h + $this->FontAscent - $this->FontDescent) / 2; break; } break; case 'D': // font bottom switch ($valign) { case 'T': // top $y -= $this->cell_padding['T'] + $this->FontAscent + $this->FontDescent; break; case 'B': // bottom $y -= $h - $this->cell_padding['B']; break; default: case 'C': case 'M': // center $y -= ($h + $this->FontAscent + $this->FontDescent) / 2; break; } break; case 'B': // cell bottom $y -= $h; break; case 'C': case 'M': // cell center $y -= $h / 2; break; default: case 'T': // cell top break; } // text vertical alignment switch ($valign) { case 'T': // top $yt = $y + $this->cell_padding['T']; break; case 'B': // bottom $yt = $y + $h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent; break; default: case 'C': case 'M': // center $yt = $y + ($h - $this->FontAscent - $this->FontDescent) / 2; break; } $basefonty = $yt + $this->FontAscent; if (TCPDF_STATIC::empty_string($w) or $w <= 0) { if ($this->rtl) { $w = $x - $this->lMargin; } else { $w = $this->w - $this->rMargin - $x; } } $s = ''; // fill and borders if (is_string($border) and strlen($border) == 4) { // full border $border = 1; } if ($fill or $border == 1) { if ($fill) { $op = $border == 1 ? 'B' : 'f'; } else { $op = 'S'; } if ($this->rtl) { $xk = ($x - $w) * $k; } else { $xk = $x * $k; } $s .= sprintf('%F %F %F %F re %s ', $xk, ($this->h - $y) * $k, $w * $k, -$h * $k, $op); } // draw borders $s .= $this->getCellBorder($x, $y, $w, $h, $border); if ($txt != '') { $txt2 = $txt; if ($this->isunicode) { $txt2 = $this->UTF8ToLatin2($txt2, $this->isunicode); } $txt2 = TCPDF_STATIC::_escape($txt2); // get current text width (considering general font stretching and spacing) $txwidth = $this->GetStringWidth($txt); $width = $txwidth; // check for stretch mode if ($stretch > 0) { // calculate ratio between cell width and text width if ($width <= 0) { $ratio = 1; } else { $ratio = ($w - $this->cell_padding['L'] - $this->cell_padding['R']) / $width; } // check if stretching is required if ($ratio < 1 or $ratio > 1 and $stretch % 2 == 0) { // the text will be stretched to fit cell width if ($stretch > 2) { // set new character spacing $this->font_spacing += ($w - $this->cell_padding['L'] - $this->cell_padding['R'] - $width) / (max($this->GetNumChars($txt) - 1, 1) * ($this->font_stretching / 100)); } else { // set new horizontal stretching $this->font_stretching *= $ratio; } // recalculate text width (the text fills the entire cell) $width = $w - $this->cell_padding['L'] - $this->cell_padding['R']; // reset alignment $align = ''; } } if ($this->font_stretching != 100) { // apply font stretching $rs .= sprintf('BT %F Tz ET ', $this->font_stretching); } if ($this->font_spacing != 0) { // increase/decrease font spacing $rs .= sprintf('BT %F Tc ET ', $this->font_spacing * $this->k); } if ($this->ColorFlag and $this->textrendermode < 4) { $s .= 'q ' . $this->TextColor . ' '; } // rendering mode $s .= sprintf('BT %d Tr %F w ET ', $this->textrendermode, $this->textstrokewidth * $this->k); // count number of spaces $ns = substr_count($txt, chr(32)); // Justification $spacewidth = 0; if ($align == 'J' and $ns > 0) { if ($this->isUnicodeFont()) { // get string width without spaces $width = $this->GetStringWidth(str_replace(' ', '', $txt)); // calculate average space width $spacewidth = -1000 * ($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns ? $ns : 1) / ($this->FontSize ? $this->FontSize : 1); if ($this->font_stretching != 100) { // word spacing is affected by stretching $spacewidth /= $this->font_stretching / 100; } // set word position to be used with TJ operator $txt2 = str_replace(chr(0) . chr(32), ') ' . sprintf('%F', $spacewidth) . ' (', $txt2); $unicode_justification = true; } else { // get string width $width = $txwidth; // new space width $spacewidth = ($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns ? $ns : 1) * $this->k; if ($this->font_stretching != 100) { // word spacing (Tw) is affected by stretching $spacewidth /= $this->font_stretching / 100; } // set word spacing $rs .= sprintf('BT %F Tw ET ', $spacewidth); } $width = $w - $this->cell_padding['L'] - $this->cell_padding['R']; } // replace carriage return characters $txt2 = str_replace("\r", ' ', $txt2); switch ($align) { case 'C': $dx = ($w - $width) / 2; break; case 'R': if ($this->rtl) { $dx = $this->cell_padding['R']; } else { $dx = $w - $width - $this->cell_padding['R']; } break; case 'L': if ($this->rtl) { $dx = $w - $width - $this->cell_padding['L']; } else { $dx = $this->cell_padding['L']; } break; case 'J': default: if ($this->rtl) { $dx = $this->cell_padding['R']; } else { $dx = $this->cell_padding['L']; } break; } if ($this->rtl) { $xdx = $x - $dx - $width; } else { $xdx = $x + $dx; } $xdk = $xdx * $k; // print text $s .= sprintf('BT %F %F Td [(%s)] TJ ET', $xdk, ($this->h - $basefonty) * $k, $txt2); if (isset($uniblock)) { // print overlapping characters as separate string $xshift = 0; // horizontal shift $ty = ($this->h - $basefonty + 0.2 * $this->FontSize) * $k; $spw = ($w - $txwidth - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns ? $ns : 1); foreach ($uniblock as $uk => $uniarr) { if ($uk % 2 == 0) { // x space to skip if ($spacewidth != 0) { // justification shift $xshift += count(array_keys($uniarr, 32)) * $spw; } $xshift += $this->GetArrStringWidth($uniarr); // + shift justification } else { // character to print $topchr = TCPDF_FONTS::arrUTF8ToUTF16BE($uniarr, false); $topchr = TCPDF_STATIC::_escape($topchr); $s .= sprintf(' BT %F %F Td [(%s)] TJ ET', $xdk + $xshift * $k, $ty, $topchr); } } } if ($this->underline) { $s .= ' ' . $this->_dounderlinew($xdx, $basefonty, $width); } if ($this->linethrough) { $s .= ' ' . $this->_dolinethroughw($xdx, $basefonty, $width); } if ($this->overline) { $s .= ' ' . $this->_dooverlinew($xdx, $basefonty, $width); } if ($this->ColorFlag and $this->textrendermode < 4) { $s .= ' Q'; } if ($link) { $this->Link($xdx, $yt, $width, $this->FontAscent + $this->FontDescent, $link, $ns); } } // output cell if ($s) { // output cell $rs .= $s; if ($this->font_spacing != 0) { // reset font spacing mode $rs .= ' BT 0 Tc ET'; } if ($this->font_stretching != 100) { // reset font stretching mode $rs .= ' BT 100 Tz ET'; } } // reset word spacing if (!$this->isUnicodeFont() and $align == 'J') { $rs .= ' BT 0 Tw ET'; } // reset stretching and spacing $this->font_stretching = $prev_font_stretching; $this->font_spacing = $prev_font_spacing; $this->lasth = $h; if ($ln > 0) { //Go to the beginning of the next line $this->y = $y + $h + $this->cell_margin['B']; if ($ln == 1) { if ($this->rtl) { $this->x = $this->w - $this->rMargin; } else { $this->x = $this->lMargin; } } } else { // go left or right by case if ($this->rtl) { $this->x = $x - $w - $this->cell_margin['L']; } else { $this->x = $x + $w + $this->cell_margin['R']; } } $gstyles = '' . $this->linestyleWidth . ' ' . $this->linestyleCap . ' ' . $this->linestyleJoin . ' ' . $this->linestyleDash . ' ' . $this->DrawColor . ' ' . $this->FillColor . "\n"; $rs = $gstyles . $rs; $this->cell_padding = $prev_cell_padding; $this->cell_margin = $prev_cell_margin; return $rs; }
/** * Writes a value * Needed to rebuild the source document * * @param mixed $value A PDF-Value. Structure of values see cases in this method */ function pdf_write_value(&$value) { switch ($value[0]) { case PDF_TYPE_STRING: if ($this->encrypted) { $value[1] = $this->_unescape($value[1]); $value[1] = $this->_encrypt_data($this->_current_obj_id, $value[1]); $value[1] = TCPDF_STATIC::_escape($value[1]); } break; case PDF_TYPE_STREAM: if ($this->encrypted) { $value[2][1] = $this->_encrypt_data($this->_current_obj_id, $value[2][1]); $value[1][1]['/Length'] = array(PDF_TYPE_NUMERIC, strlen($value[2][1])); } break; case PDF_TYPE_HEX: if ($this->encrypted) { $value[1] = $this->hex2str($value[1]); $value[1] = $this->_encrypt_data($this->_current_obj_id, $value[1]); // remake hexstring of encrypted string $value[1] = $this->str2hex($value[1]); } break; } switch ($value[0]) { case PDF_TYPE_TOKEN: $this->_straightOut('/' . $value[1] . ' '); break; case PDF_TYPE_NUMERIC: case PDF_TYPE_REAL: if (is_float($value[1]) && $value[1] != 0) { $this->_straightOut(rtrim(rtrim(sprintf('%F', $value[1]), '0'), '.') . ' '); } else { $this->_straightOut($value[1] . ' '); } break; case PDF_TYPE_ARRAY: // An array. Output the proper // structure and move on. $this->_straightOut('['); for ($i = 0; $i < count($value[1]); $i++) { $this->pdf_write_value($value[1][$i]); } $this->_out(']'); break; case PDF_TYPE_DICTIONARY: // A dictionary. $this->_straightOut('<<'); reset($value[1]); while (list($k, $v) = each($value[1])) { $this->_straightOut($k . ' '); $this->pdf_write_value($v); } $this->_straightOut('>>'); break; case PDF_TYPE_OBJREF: // An indirect object reference // Fill the object stack if needed $cpfn =& $this->current_parser->uniqueid; if (!isset($this->_don_obj_stack[$cpfn][$value[1]])) { $this->_newobj(false, true); $this->_obj_stack[$cpfn][$value[1]] = array($this->n, $value); $this->_don_obj_stack[$cpfn][$value[1]] = array($this->n, $value); // Value is maybee obsolete!!! } $objid = $this->_don_obj_stack[$cpfn][$value[1]][0]; $this->_out($objid . ' 0 R'); break; case PDF_TYPE_STRING: // A string. $this->_straightOut('(' . $value[1] . ')'); break; case PDF_TYPE_STREAM: // A stream. First, output the // stream dictionary, then the // stream data itself. $this->pdf_write_value($value[1]); $this->_out('stream'); $this->_out($value[2][1]); $this->_out('endstream'); break; case PDF_TYPE_HEX: $this->_straightOut('<' . $value[1] . '>'); break; case PDF_TYPE_BOOLEAN: $this->_straightOut($value[1] ? 'true ' : 'false '); break; case PDF_TYPE_NULL: // The null object. $this->_straightOut('null '); break; } }