First of all, I apologize for the lateness in this reply. Christmas break can do that sometimes. ;)
I do know that, in the composition of the pages on my site, there are multiple stylesheets I have to invoke. The key one... gumby.css ... has to be invoked like so:
<link rel="stylesheet" href="../css/gumby.css" />
Is this because I do not compose the site with any tables? It's a DIV-powered website with a framework that discourages tables as the main source of page composition.
Minus the username and password, here are the settings on the dompdf_config.custom.inc.php file:
<?php
define("DOMPDF_TEMP_DIR", "/tmp");
define("DOMPDF_CHROOT", DOMPDF_DIR);
define("DOMPDF_FONT_DIR", DOMPDF_DIR."/lib/fonts/");
define("DOMPDF_FONT_CACHE", DOMPDF_DIR."/lib/fonts/");
define("DOMPDF_UNICODE_ENABLED", true);
define("DOMPDF_PDF_BACKEND", "CPDF");
define("DOMPDF_DEFAULT_MEDIA_TYPE", "print");
define("DOMPDF_DEFAULT_PAPER_SIZE", "letter");
define("DOMPDF_DEFAULT_FONT", "Roboto");
//define("DOMPDF_DEFAULT_FONT", "serif");
define("DOMPDF_DPI", 72);
define("DOMPDF_ENABLE_PHP", true);
define("DOMPDF_ENABLE_REMOTE", true);
define("DOMPDF_ENABLE_CSS_FLOAT", true);
define("DOMPDF_ENABLE_JAVASCRIPT", true);
define("DEBUGPNG", false);
define("DEBUGKEEPTEMP", false);
define("DEBUGCSS", false);
define("DEBUG_LAYOUT", false);
define("DEBUG_LAYOUT_LINES", false);
define("DEBUG_LAYOUT_BLOCKS", false);
define("DEBUG_LAYOUT_INLINE", false);
define("DOMPDF_FONT_HEIGHT_RATIO", 1.0);
define("DEBUG_LAYOUT_PADDINGBOX", false);
define("DOMPDF_LOG_OUTPUT_FILE", DOMPDF_FONT_DIR."log.htm");
define("DOMPDF_ENABLE_HTML5PARSER", true);
define("DOMPDF_ENABLE_FONTSUBSETTING", true);
[...]
As for the custom font not being called, I rewrote the dompdf_config.custom.inc.php file to accept "Roboto" as the default font (which is the site's default font to begin with).
And then, back to the main point. It actually would be best to utlize the second option you mentioned:
"If that's not feasible you could write a script that parses a page for the printReady content (e.g. using DOMDocument) and uses a pre-defined stylesheet or grabs the head content to apply the document-defined styles." Because, there are sections of our website that are secured through a log in process, with all the necessary pages tagged at the top with a redirect to the log in page if the user ID isn't passed through.
Like this. But how do I do that, and with what file? Is it just with the dompdf.php file, or do I do this to every single page to export a PDF from? I have little to no experience with DOMDocument, but I am assuming
this is what you are referring to?
Here is the dompdf.php file in full:
/**
* Display command line usage
*/
function dompdf_usage() {
$default_paper_size = DOMPDF_DEFAULT_PAPER_SIZE;
echo <<<EOD
Usage: {$_SERVER["argv"][0]} [options] html_file
html_file can be a filename, a url if fopen_wrappers are enabled, or the '-' character to read from standard input.
Options:
-h Show this message
-l List available paper sizes
-p size Paper size; something like 'letter', 'A4', 'legal', etc.
The default is '$default_paper_size'
-o orientation Either 'portrait' or 'landscape'. Default is 'portrait'
-b path Set the 'document root' of the html_file.
Relative urls (for stylesheets) are resolved using this directory.
Default is the directory of html_file.
-f file The output filename. Default is the input [html_file].pdf
-v Verbose: display html parsing warnings and file not found errors.
-d Very verbose: display oodles of debugging output: every frame
in the tree printed to stdout.
-t Comma separated list of debugging types (page-break,reflow,split)
EOD;
exit;
}
/**
* Parses command line options
*
* @return array The command line options
*/
function getoptions() {
$opts = array();
if ( $_SERVER["argc"] == 1 )
return $opts;
$i = 1;
while ($i < $_SERVER["argc"]) {
switch ($_SERVER["argv"][$i]) {
case "--help":
case "-h":
$opts["h"] = true;
$i++;
break;
case "-l":
$opts["l"] = true;
$i++;
break;
case "-p":
if ( !isset($_SERVER["argv"][$i+1]) )
die("-p switch requires a size parameter\n");
$opts["p"] = $_SERVER["argv"][$i+1];
$i += 2;
break;
case "-o":
if ( !isset($_SERVER["argv"][$i+1]) )
die("-o switch requires an orientation parameter\n");
$opts["o"] = $_SERVER["argv"][$i+1];
$i += 2;
break;
case "-b":
if ( !isset($_SERVER["argv"][$i+1]) )
die("-b switch requires a path parameter\n");
$opts["b"] = $_SERVER["argv"][$i+1];
$i += 2;
break;
case "-f":
if ( !isset($_SERVER["argv"][$i+1]) )
die("-f switch requires a filename parameter\n");
$opts["f"] = $_SERVER["argv"][$i+1];
$i += 2;
break;
case "-v":
$opts["v"] = true;
$i++;
break;
case "-d":
$opts["d"] = true;
$i++;
break;
case "-t":
if ( !isset($_SERVER['argv'][$i + 1]) )
die("-t switch requires a comma separated list of types\n");
$opts["t"] = $_SERVER['argv'][$i+1];
$i += 2;
break;
default:
$opts["filename"] = $_SERVER["argv"][$i];
$i++;
break;
}
}
return $opts;
}
require_once("dompdf_config.inc.php");
global $_dompdf_show_warnings, $_dompdf_debug, $_DOMPDF_DEBUG_TYPES;
$sapi = php_sapi_name();
$options = array();
switch ( $sapi ) {
case "cli":
$opts = getoptions();
if ( isset($opts["h"]) || (!isset($opts["filename"]) && !isset($opts["l"])) ) {
dompdf_usage();
exit;
}
if ( isset($opts["l"]) ) {
echo "\nUnderstood paper sizes:\n";
foreach (array_keys(CPDF_Adapter::$PAPER_SIZES) as $size)
echo " " . mb_strtoupper($size) . "\n";
exit;
}
$file = $opts["filename"];
if ( isset($opts["p"]) )
$paper = $opts["p"];
else
$paper = DOMPDF_DEFAULT_PAPER_SIZE;
if ( isset($opts["o"]) )
$orientation = $opts["o"];
else
$orientation = "portrait";
if ( isset($opts["b"]) )
$base_path = $opts["b"];
if ( isset($opts["f"]) )
$outfile = $opts["f"];
else {
if ( $file === "-" )
$outfile = "dompdf_out.pdf";
else
$outfile = str_ireplace(array(".html", ".htm", ".php"), "", $file) . ".pdf";
}
if ( isset($opts["v"]) )
$_dompdf_show_warnings = true;
if ( isset($opts["d"]) ) {
$_dompdf_show_warnings = true;
$_dompdf_debug = true;
}
if ( isset($opts['t']) ) {
$arr = split(',',$opts['t']);
$types = array();
foreach ($arr as $type)
$types[ trim($type) ] = 1;
$_DOMPDF_DEBUG_TYPES = $types;
}
$save_file = true;
break;
default:
if ( isset($_GET["input_file"]) )
$file = rawurldecode($_GET["input_file"]);
else
throw new DOMPDF_Exception("An input file is required (i.e. input_file _GET variable).");
if ( isset($_GET["paper"]) )
$paper = rawurldecode($_GET["paper"]);
else
$paper = DOMPDF_DEFAULT_PAPER_SIZE;
if ( isset($_GET["orientation"]) )
$orientation = rawurldecode($_GET["orientation"]);
else
$orientation = "portrait";
if ( isset($_GET["base_path"]) ) {
$base_path = rawurldecode($_GET["base_path"]);
$file = $base_path . $file; # Set the input file
}
if ( isset($_GET["options"]) ) {
$options = $_GET["options"];
}
$file_parts = explode_url($file);
/* Check to see if the input file is local and, if so, that the base path falls within that specified by DOMDPF_CHROOT */
if(($file_parts['protocol'] == '' || $file_parts['protocol'] === 'file://')) {
$file = realpath($file);
if ( strpos($file, DOMPDF_CHROOT) !== 0 ) {
throw new DOMPDF_Exception("Permission denied on $file. The file could not be found under the directory specified by DOMPDF_CHROOT.");
}
}
$outfile = "dompdf_out.pdf"; # Don't allow them to set the output file
$save_file = false; # Don't save the file
break;
}
$dompdf = new DOMPDF();
if ( $file === "-" ) {
$str = "";
while ( !feof(STDIN) )
$str .= fread(STDIN, 4096);
$dompdf->load_html($str);
} else
$dompdf->load_html_file($file);
if ( isset($base_path) ) {
$dompdf->set_base_path($base_path);
// $dompdf->set_base_path(realpath(APPLICATION_PATH . '/css/gumby.css')); (I tried this line of code originally, but since removed it after it proved to be of no help at all.)
}
$dompdf->set_paper($paper, $orientation);
$dompdf->render();
if ( $_dompdf_show_warnings ) {
global $_dompdf_warnings;
foreach ($_dompdf_warnings as $msg)
echo $msg . "\n";
echo $dompdf->get_canvas()->get_cpdf()->messages;
flush();
}
if ( $save_file ) {
// if ( !is_writable($outfile) )
// throw new DOMPDF_Exception("'$outfile' is not writable.");
if ( strtolower(DOMPDF_PDF_BACKEND) === "gd" )
$outfile = str_replace(".pdf", ".png", $outfile);
list($proto, $host, $path, $file) = explode_url($outfile);
if ( $proto != "" ) // i.e. not file://
$outfile = $file; // just save it locally, FIXME? could save it like wget: ./host/basepath/file
$outfile = realpath(dirname($outfile)) . DIRECTORY_SEPARATOR . basename($outfile);
if ( strpos($outfile, DOMPDF_CHROOT) !== 0 )
throw new DOMPDF_Exception("Permission denied.");
file_put_contents($outfile, $dompdf->output( array("compress" => 0) ));
exit(0);
}
if ( !headers_sent() ) {
$dompdf->stream($outfile, $options);
}