How do I include print.css to the other style sheets in page.tpl.php (see below).

<head>
  <title><?php print $head_title; ?></title>
  <?php print $head; ?>
  <strong><?php print $styles; ?></strong>
  <?php print $scripts; ?>
</head>

Thanks

Russ

Comments

Chill35’s picture

What is the path to your stylesheet ?

Suppose that print.css is, relative to index.php, in themes/myTheme/

Here is what I would do in page.tpl.php

<head>
<title><?php print $head_title; ?></title>
<?php print $head; ?>
<?php print $styles; ?>
<style type="text/css" media="print">@import "<?php echo base_path(); ?>themes/myTheme/print.css";</style>
<?php print $scripts; ?>
</head>

Caroline

ps : I'd add it just after all the sytlesheets
ps2 : don't forget media="print". That tells the browser to use the stylesheet only when user prints the page...

samkuehn’s picture

Don't you have to get print $styles to say media="screen" instead of media="all" for you themes style.css? Does anybody know how to do this?

Chill35’s picture

Oh. That's a good one.

Usually you'd have a print.css that'd override, using CSS specificity, the rules specified in other stylesheets that have media set to "all".

In the case where this is very hard to achieve, and you would very much prefer to start from scratch in defining CSS rules for printing, that is, you would like to set other stylesheets media attribute to "screen" rather than "all", then here's what you can do :

FIRST : Link in your print.css stylesheet in page.tpl.php (in your theme folder).

<style type="text/css" media="print">@import "<?php print base_path() . path_to_theme() ?>/print.css";</style>

(It's better to use the Drupal function path_to_theme(), so disregard what I had said before.)

SECOND : Modify the function theme_add_style($path = '', $media = 'all') in includes/theme.inc, lines 331-341 (in Drupal 4.7.5).

function theme_add_style($path = '', $media = 'screen') {
  static $styles = array();

  if ($path && !isset($styles["$media:$path"])) {
    $style = new stdClass();
    $style->path = base_path() . $path;
    $style->media = $media;
    $styles["$media:$path"] = $style;
  }
  return $styles;
}

That is the same code as in the original theme_add_style(), except that it gets a default "screen" value for media, instead of "all", in the very first line of the code above.

The function theme_add_style is not a themeing function, it cannot be overriden in template.php

Take note of the change you make in theme.inc because it's a change in core, so next time time you upgrade, your change will be wiped out. It's very much preferable to write a print.css stylesheet that can work with other stylesheets, linked "in" after the other stylesheets, using CSS specificity.

Caroline

talthompson’s picture

How would this apply to Drupal 6?

peterh’s picture

remove print $print['css'] from the section of your print.tpl.php

and add

<link type='text/css' rel='stylesheet' media='all' href='/sites/your_site.com/themes/your_theme_dir/your_print.css' />

optionally change

media='all'

into

media='print'

that's it!

jsims281’s picture

This is a very old thread but I thought I'd put an answer here in case anyone finds it useful:

If you add the css file to your themename.info file like this:

stylesheets[print][] = "css/print.css"    

Make sure the file actually exists, and that you clear the cache. It should appear with the other stylesheets as expected.

talthompson’s picture

how would this apply to Drupal 6?

mvantuch’s picture

In case you need to use the #attached:

$element['#attached']['css'] = ['path/to/css' => ['media' => 'print']];