Page_Cache

Caches individual rendered PDF pages.

Defined (1)

The class is defined in the following location(s).

/lib/dompdf/include/page_cache.cls.php  
  1. class Page_Cache { 
  2.  
  3. const DB_USER = "dompdf_page_cache"; 
  4. const DB_PASS = "some meaningful password"; 
  5. const DB_NAME = "dompdf_page_cache"; 
  6.  
  7. static private $__connection = null; 
  8.  
  9. static function init() { 
  10. if ( is_null(self::$__connection) ) { 
  11. $con_str = "host=" . DB_HOST . 
  12. " dbname=" . self::DB_NAME . 
  13. " user=" . self::DB_USER . 
  14. " password=" . self::DB_PASS; 
  15.  
  16. if ( !self::$__connection = pg_connect($con_str) ) 
  17. throw new Exception("Database connection failed."); 
  18.  
  19. function __construct() { throw new Exception("Can not create instance of Page_Class. Class is static."); } 
  20.  
  21. private static function __query($sql) { 
  22. if ( !($res = pg_query(self::$__connection, $sql)) ) 
  23. throw new Exception(pg_last_error(self::$__connection)); 
  24. return $res; 
  25.  
  26. static function store_page($id, $page_num, $data) { 
  27. $where = "WHERE id='" . pg_escape_string($id) . "' AND ". 
  28. "page_num=". pg_escape_string($page_num); 
  29.  
  30. $res = self::__query("SELECT timestamp FROM page_cache ". $where); 
  31.  
  32. $row = pg_fetch_assoc($res); 
  33.  
  34. if ( $row )  
  35. self::__query("UPDATE page_cache SET data='" . pg_escape_string($data) . "' " . $where); 
  36. else  
  37. self::__query("INSERT INTO page_cache (id, page_num, data) VALUES ('" . pg_escape_string($id) . "', ". 
  38. pg_escape_string($page_num) . ", ". 
  39. "'". pg_escape_string($data) . "')"); 
  40.  
  41.  
  42. static function store_fonts($id, $fonts) { 
  43. self::__query("BEGIN"); 
  44. // Update the font information 
  45. self::__query("DELETE FROM page_fonts WHERE id='" . pg_escape_string($id) . "'"); 
  46.  
  47. foreach (array_keys($fonts) as $font) 
  48. self::__query("INSERT INTO page_fonts (id, font_name) VALUES ('" . 
  49. pg_escape_string($id) . "', '" . pg_escape_string($font) . "')"); 
  50. self::__query("COMMIT"); 
  51.  
  52. // static function retrieve_page($id, $page_num) { 
  53.  
  54. // $res = self::__query("SELECT data FROM page_cache WHERE id='" . pg_escape_string($id) . "' AND ". 
  55. // "page_num=". pg_escape_string($page_num)); 
  56.  
  57. // $row = pg_fetch_assoc($res); 
  58.  
  59. // return pg_unescape_bytea($row["data"]); 
  60.  
  61. // } 
  62.  
  63. static function get_page_timestamp($id, $page_num) { 
  64. $res = self::__query("SELECT timestamp FROM page_cache WHERE id='" . pg_escape_string($id) . "' AND ". 
  65. "page_num=". pg_escape_string($page_num)); 
  66.  
  67. $row = pg_fetch_assoc($res); 
  68.  
  69. return $row["timestamp"]; 
  70.  
  71.  
  72. // Adds the cached document referenced by $id to the provided pdf 
  73. static function insert_cached_document(CPDF_Adapter $pdf, $id, $new_page = true) { 
  74. $res = self::__query("SELECT font_name FROM page_fonts WHERE id='" . pg_escape_string($id) . "'"); 
  75.  
  76. // Ensure that the fonts needed by the cached document are loaded into 
  77. // the pdf 
  78. while ($row = pg_fetch_assoc($res))  
  79. $pdf->get_cpdf()->selectFont($row["font_name"]); 
  80.  
  81. $res = self::__query("SELECT data FROM page_cache WHERE id='" . pg_escape_string($id) . "'"); 
  82.  
  83. if ( $new_page ) 
  84. $pdf->new_page(); 
  85.  
  86. $first = true; 
  87. while ($row = pg_fetch_assoc($res)) { 
  88.  
  89. if ( !$first )  
  90. $pdf->new_page(); 
  91. else  
  92. $first = false;  
  93.  
  94. $page = $pdf->reopen_serialized_object($row["data"]); 
  95. //$pdf->close_object(); 
  96. $pdf->add_object($page, "add"); 
  97.  
  98.