Implementation with new window and server based content. Prints better in Safari.
authorMichael Ang <mang@archive.org>
Wed, 23 Sep 2009 01:21:57 +0000 (01:21 +0000)
committerMichael Ang <mang@archive.org>
Wed, 23 Sep 2009 01:21:57 +0000 (01:21 +0000)
GnuBook/GnuBook.js
GnuBookIA/www/print.php [new file with mode: 0644]

index ba5c0f5..24c8df3 100644 (file)
@@ -2396,6 +2396,10 @@ GnuBook.prototype.removeSearchHilites = function() {
 // printPage
 //______________________________________________________________________________
 GnuBook.prototype.printPage = function() {
+    window.open(this.getPrintURI(), 'printpage', 'width=400, height=500, resizable=yes, scrollbars=no, toolbar=no, location=no');
+
+    /* iframe implementation
+
     if (null != this.printPopup) { // check if already showing
         return;
     }
@@ -2448,6 +2452,28 @@ GnuBook.prototype.printPage = function() {
     });
     
     $('#printDiv').prepend(iframe);
+    */
+}
+
+// Get print URI from current indices and mode
+GnuBook.prototype.getPrintURI = function() {
+    var indexToPrint;
+    if (this.constMode2up == this.mode) {
+        indexToPrint = this.twoPage.currentIndexL;        
+    } else {
+        indexToPrint = this.firstIndex; // $$$ the index in the middle of the viewport would make more sense
+    }
+    
+    var options = 'id=' + this.bookId + '&server=' + this.server + '&zip=' + this.zip
+        + '&format=' + this.imageFormat + '&index=' + this.leafMap[indexToPrint]
+        + '&width=' + this.getPageWidth(indexToPrint) + '&height=' + this.getPageHeight(indexToPrint);
+   
+    if (this.constMode2up == this.mode) {
+        options += '&index2=' + this.leafMap[this.twoPage.currentIndexR] + '&width2=' + this.getPageWidth(this.twoPage.currentIndexR);
+        options += '&height2=' + this.getPageHeight(this.twoPage.currentIndexR);
+    }
+
+    return '/bookreader/print.php?' + options;
 }
 
 GnuBook.prototype.getPrintFrameContent = function(index) {    
diff --git a/GnuBookIA/www/print.php b/GnuBookIA/www/print.php
new file mode 100644 (file)
index 0000000..243bade
--- /dev/null
@@ -0,0 +1,94 @@
+<?
+/*
+Copyright(c)2008 Internet Archive. Software license AGPL version 3.
+
+This file is part of GnuBook.
+
+    GnuBook is free software: you can redistribute it and/or modify
+    it under the terms of the GNU Affero General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    GnuBook is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU Affero General Public License for more details.
+
+    You should have received a copy of the GNU Affero General Public License
+    along with GnuBook.  If not, see <http://www.gnu.org/licenses/>.
+
+GnuBookPrint.php exists to get around the same-origin policy that prevents
+us from calling print() on an iframe that comes from a cluster datanode.
+*/
+
+$id     = $_REQUEST['id'];
+$server = $_REQUEST['server'];
+$zip    = $_REQUEST['zip'];
+$index  = $_REQUEST['index'];
+$format = $_REQUEST['format'];
+//$imgAspect = $_REQUEST['aspect'];
+$width = $_REQUEST['width'];
+$height = $_REQUEST['height'];
+
+// $$$ escape the values
+
+/* We assume that the print aspect ratio is somewhat close to US Letter in portrait orientation */
+$paperAspect = 8.5/11;
+$imgAspect = $width / $height;
+
+// Returns (url, attrs)
+function imageURL($paperAspect, $index, $format, $width, $height) {
+    global $server, $id, $zip;
+    
+    $rotate = "0";
+    $imgAspect = $width / $height;
+    
+    if ('jp2' == $format) {
+        // Rotation is possible
+        if ($imgAspect > $paperAspect) {
+            $rotate = "90";
+            $imgAspect = 1 / $imgAspect;
+        }
+    }
+    
+    if ($imgAspect > $paperAspect) {
+        // wider than paper, fit width
+        $htmlAttrs = "width='100%'";
+    } else {
+        // taller than paper, fit height
+        $htmlAttrs = "height='100%'";
+    }
+
+    $file = sprintf("%s_%s/%s_%04d.%s", $id, $format, $id, $index, $format);
+    
+    $queryParams = array(
+        'id' => $id, // global
+        'format' => $format,
+        'index' => $index,
+        'file' => $file,
+        'zip' => $zip, // global
+        'rotate' => $rotate,
+        'scale' => 1
+    );
+
+    return "<img src='http://{$server}/GnuBook/GnuBookImages.php?" . http_build_query($queryParams) . "' " . $htmlAttrs . " />";
+}
+
+echo "<html><head><title>" . $id . "</title><body onload='print(); return false;'>";
+echo   "<p style='text-align:center;'>";
+echo     imageURL($paperAspect, $index, $format, $width, $height);
+echo   "</p>";
+
+if (isset($_REQUEST['index2'])) {    
+    $index2 = $_REQUEST['index2'];
+    $width2 = $_REQUEST['width2'];
+    $height2 = $_REQUST['height2'];
+    
+    
+    echo "<p style='text-align: center;'>";
+    echo imageURL($paperAspect, $index2, $format, $width2, $height2);
+    echo "</p>";
+}
+echo  "</body></html>";
+
+?>