*** tcpdf.php.orig	2009-06-11 16:01:59.000000000 +0200
--- tcpdf.php	2009-06-11 16:04:34.000000000 +0200
***************
*** 146,152 ****
   */
  require_once(dirname(__FILE__).'/htmlcolors.php');
  
! if (!class_exists('TCPDF', false)) {
  	/**
  	 * define default PDF document producer
  	 */ 
--- 146,152 ----
   */
  require_once(dirname(__FILE__).'/htmlcolors.php');
  
! if (!class_exists('TCPDF')) {
  	/**
  	 * define default PDF document producer
  	 */ 
***************
*** 653,658 ****
--- 653,711 ----
  		 * @access protected
  		 */
  		protected $HREF = array();
+ 		
+                 /**^M
+                  * @var HTML PARSER: store current ancher.^M
+                  * @access protected^M
+                  */
+                 protected $NAME = NULL;
+                 /**^M
+                   * Mark() is automatically called on A HTML tag with NAME.^M
+                   * Function may be redefined.^M
+                   * @param string $string, value taken from name in A tag.^M
+                   * returns array of string and level for Bookmark.^M
+                   * level -1 will not do the bookmark inclusion in pdf.^M
+                   */
+                 public function Mark( $strg = '', $level = '' ) {
+                       return $level == '' ? -1 : 0 ;
+                 }
+ 		/**
+ 		 * HnTitle searches for title text of the Hn tag
+ 		 * returns array with string and level
+ 		 */
+ 		private function HnTitle( &$dom, $key, $aname = '' ) {
+ 		    static $min = -1;
+ 		    if( !array_key_exists('parent',$dom[$key]) ) return NULL;
+ 		    if( !$dom[$key]['tag'] AND !$dom[$key]['opening']) return NULL;
+ 		    if( substr($dom[$key]['value'],0,1) != 'h' ) return NULL;
+ 		    $str = '';
+ 		    for($i = $key; array_key_exists($i,$dom); $i++ ) {
+ 			if( $dom[$i]['tag'] AND !$dom[$i]['opening']
+ 			    AND $dom[$i]['value']==$dom[$key]['value'] )
+ 			    break;
+ 			if( $dom[$i]['tag'] AND $dom[$i]['opening'] )
+ 			    switch($dom[$i]['value']){
+ 			    case 'img': case 'br': case 'b': case 'i': case 'tt':
+ 			    case 'u': case 'strike': case 'big': case 'small':
+ 			    case 'sup': case 'sub': case 'font': case 'acronym':
+ 			    case 'cite': case 'code': case 'dfn': case 'em': case 'kbd':
+ 			    case 'samp': case 'strong': case 'var': case 'a':
+ 				continue;
+ 			    default: break;
+ 			}
+ 			if( $dom[$i]['parent'] < $key ) continue;
+ 			if( !$dom[$i]['tag'] AND isset($dom[$i]['value']) )
+ 			    $str .= ' '.trim($dom[$i]['value']); $str = trim($str);
+ 		    }
+ 		    // antipate on incorrect Hn size usage
+ 		    if( $min < 0 ) $min = intval(substr($dom[$key]['value'],1,2));
+ 		    $level = $this->Mark($str, $aname); // is there  function to provide level info?
+ 		    if( $level < 0 ) { // no there is not
+ 		        $level = intval(substr($dom[$key]['value'],1,2))-$min;
+ 			if( $level < 0 ) $min += $level;
+ 		    }
+ 		    return array( $str, $level );
+ 		}
  		
  		/**
  		 * @var store a list of available fonts on filesystem.
***************
*** 1396,1401 ****
--- 1449,1455 ----
  			$this->PDFVersion = '1.7';
  			$this->encoding = $encoding;
  			$this->HREF = array();
+ 			$this->NAME = NULL;
  			$this->getFontsList();
  			$this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
  			$this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
***************
*** 3218,3226 ****
--- 3272,3282 ----
  				}
  			}
  			$this->PageAnnots[$this->page][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
+ 			if ( array_key_exists('Subtype',$opt) AND !empty( $opt['Subtype'] ) ) {
  			if (($opt['Subtype'] == 'FileAttachment') AND (!$this->empty_string($opt['FS'])) AND file_exists($opt['FS']) AND (!isset($this->embeddedfiles[basename($opt['FS'])]))) {
  				$this->embeddedfiles[basename($opt['FS'])] = array('file' => $opt['FS'], 'n' => (count($this->embeddedfiles) + 100000));
  			}
+ 			}
  		}
  
  		/**
***************
*** 11933,11939 ****
--- 11989,11997 ----
  					$strrest = '';
  					if (!empty($this->HREF) AND (isset($this->HREF['url']))) {
  						// HTML <a> Link
+ 						if( array_key_exists('url',$this->HREF) AND !empty( $this->HREF['url'] ) ) {
  						$strrest = $this->addHtmlLink($this->HREF['url'], $dom[$key]['value'], $wfill, true, $this->HREF['color'], $this->HREF['style']);
+ 						}
  					} else {
  						$ctmpmargin = $this->cMargin;
  						$this->cMargin = 0;
***************
*** 12114,12119 ****
--- 12172,12181 ----
  					break;
  				}
  				case 'a': {
+ 					if( $this->NAME == NULL AND array_key_exists('name', $tag['attribute']) )
+                                                  $this->NAME = $this->HnTitle($dom,$dom[$key]['parent'],$tag['attribute']['name'], $tag['attribute']['name']);
+ 					if( $this->NAME == NULL AND array_key_exists('id', $tag['attribute']) )
+                                                  $this->NAME = $this->HnTitle($dom,$dom[$key]['parent'],$tag['attribute']['id'], $tag['attribute']['id']);
  					if (array_key_exists('href', $tag['attribute'])) {
  						$this->HREF['url'] = $tag['attribute']['href'];
  					}
***************
*** 12697,12702 ****
--- 12759,12771 ----
  				case 'h4': 
  				case 'h5': 
  				case 'h6': {
+                                       if( $this->NAME == NULL )
+                                           $this->NAME = $this->HnTitle($dom,$key);
+                                       if ($this->NAME ) {
+                                               // HTML ancher or title found
+                                               $this->Bookmark($this->NAME[0],$this->NAME[1]);
+                                               $this->NAME = NULL;
+                                       }
  					$this->addHTMLVertSpace(1, $cell, ($parent['fontsize'] * 1.5) / $this->k, $firstorlast, $tag['value'], true);
  					break;
  				}
