Incorporate external visuals into printed materials

When a page is printed, a heading should be accompanied by an image to the right. I have defined the following stylesheet:

@media print {
  h1::after {
    content: url( IMAGE_URL_HERE );
    position: absolute;
    top: 0;
    right: 0;
  }
}

The issue arises when the image does not appear in the print preview of any browser. However, if the print preview dialog is closed and reopened, the image then displays correctly.

To replicate this problem yourself, you can visit: http://jsbin.com/gevefivi

Is there an error on my end causing this or is it expected behavior?

UPDATE: It seems that this may be due to a race condition within the printing engine's layout rendering process, as local images always display in Print Preview and are printed without issue.

UPDATE: It appears that the print engine does not wait for external resources in content: url() to load before generating the PDF file for preview. This explains why the image may not appear initially if the fetch process takes some time to complete. The print engine does send an HTTP request and saves the resource locally for future use, which clarifies why the image usually displays properly upon reopening Print Preview.

I conducted a test using a PHP file as an external resource:

<?php

sleep(5);

// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);

// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

// Output the image
imagejpeg($im);

// Free up memory
imagedestroy($im);

This PHP file only returns an image after 5 seconds. To recreate the issue, ensure the PHP file is accessible on your local server or network. Reference it in content: url(), open the Print Preview dialog, and observe that the image does not appear. Close the dialog, reopen it, and the image still may not display. Wait 2-3 seconds, reopen the dialog, and the image should now be visible as it has been fetched and cached. Removing the sleep(5); line should result in the image displaying immediately upon opening Print Preview.

I am curious to determine whether this is a bug or simply expected behavior, and if there is a viable solution currently available.

Answer №1

After conducting extensive experiments, I have reached the conclusion that modern web browsers do not wait for external resources specified in print-specific stylesheets to finish downloading before rendering the print-ready document.

Therefore, it appears that the only way to include external resources in the print version of a webpage is to ensure they are downloaded beforehand (via HTML tags, screen stylesheets, or JavaScript) prior to the user initiating the print process.

Interestingly enough, this behavior seems to be more of a standard practice accepted by major browser vendors rather than a bug.

Answer №2

Hey there, I totally understand where you're coming from! I actually came up with a little hack to preload images using CSS: It's not the most efficient way since it loads the images every time though :(

If I use your code example:

print.css

.h1:before {
      content: url(IMAGE_URL_HERE); 
}

screen.css

h1 {
       background: url(IMAGE_URL_HERE) no-repeat -9999px -9999px; 
      //Image not visible but still loaded
}

Thanks for sharing!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

bulk purchase discount with HTML/JavaScript/PHP

I am looking to add a slider feature to my "Prices" page in order to provide customers with an instant quote based on the quantity they select. The slider should range from 1 to 500 or even up to 1000, but having an input box for customers to enter the qu ...

Customize JqGrid Edit/Delete button CSS style

Within my JQGrid, I have successfully implemented Edit and Delete buttons that redirect users to another page for editing or deleting specific records. However, I am facing a CSS issue with the styling of these buttons. How can I override the current style ...

Troubleshooting issue with CSS `gap` property malfunctioning in conjunction with `display: block;` setting

I've run into an issue with the gap property in CSS not working as expected in my layout. After investigating, I found that some of my containers have display: block; set, which seems to be causing the gap property to be ineffective. My goal is to cre ...

What's the difference in width between Inline-Block and Float?

HTML <ul> <li><a href="#">Item #1</a></li> <li><a href="#">Item #2</a></li> <li><a href="#">Item #3</a></li> <li><a href="#">Item #4</a></li> & ...

What is the best way to manipulate the size and orientation of an image upon hovering over

I'm struggling to figure out how to simultaneously rotate and scale a .png file when the user hovers over it. Currently, it only scales when I hover on the picture. I understand that the scale property overwrites the first transform but I can't s ...

An inexplicable right margin

I am facing an issue with centering a table (a tracklist) under the album cover and title. There seems to be an unexpected margin on the right side that I can't seem to figure out. Any help in identifying the cause of this issue would be greatly appre ...

Modify the size of a div based on the status of a checkbox using only HTML and CSS

Here is a small project that I created using only HTML & CSS. The goal is to modify the properties of the <div class="pro"> when the checkbox is checked. When this happens, all other <div class="pro"> elements should be hidden and the <art ...

Set a background image URL for a specific division inside a template

I have a drawPlaceDetails function that populates an HTML template with values. The getPhotoURL() function retrieves a URL link to an image, which I am trying to assign to the background-image property. However, the div remains empty without displaying the ...

Creating a design with three parallel bars in CSS

For my webpage, I wanted to add an extended flag at the top by creating three horizontal stripes: CSS #main1 div{ width: auto; height: 20px; margin: 0px HTML <div id="main1"> <div style="background-color:red;"></div> <div style="ba ...

Guide on storing images in a designated folder using CodeIgniter

My code is located in view/admin_view2.php <?php echo form_open_multipart('home_admin/createBerita'); ?> <div class="form-group" > <label class="control-label">upload foto</label> <inpu ...

an occurrence of an element being rendered invisible, within an instance of an element being rendered as flexible,

I am trying to create a button that will show a list of elements. Within this list, there is another button that can be used to either close the list or hide it entirely. However, for some reason, my code is not functioning properly. let btn1 = documen ...

Position the label to the left of the form-switch using Bootstrap

I have been struggling to align the label to the left of my switchbox. Despite searching through stackoverflow and other websites, I haven't been successful in achieving the desired alignment. Here is a trimmed down version of the code for reference: ...

Is there anyone who can assist in refining the padding and margin issues within this Galleria.js implementation?

I am struggling to understand why the padding and margins around the images in this slideshow theme on PregoApp are uneven. Despite my attempts to adjust the CSS, the issue persists. You can view the site here. Another website using the same implementati ...

Styling buttons with CSS in the Bootstrap framework

I am facing an issue with customizing buttons in Bootstrap. <div class="btn-group"> <button class="btn btn-midd">Link</button> <button class="btn btn-midd dropdown-toggle"> <span class="icon-download-alt icon-gray ...

Eliminate spacing between divs of varying heights

I'm facing an issue with my small gallery of images. Despite having the same width, they vary in height causing the second row to start below the tallest image from the previous row. How can I eliminate these empty spaces while still maintaining the v ...

The clickable functionality of buttons and text labels in Vue is currently not working

When I try to input text into the registration page in vue, nothing happens when I click the register/login button. However, if I press TAB and then enter my password followed by another TAB and ENTER, it works. This is the code snippet for the login-box: ...

Is it feasible to create a translucent overlay that sits atop another element without hindering or intercepting mouse clicks using CSS and JavaScript?

I'm looking to overlay one layer on top of another without capturing input. My aim is to place a PNG image over my webpage in order to achieve a filter-like effect. The issue I'm facing is that when the overlay is in place, the text becomes uns ...

Utilizing Selenium to extract data from a table and displaying the results

I am looking to scrape tables from a specific website, but I am new to automation with Selenium. Here is the code snippet that I have come up with after doing some research: from selenium.webdriver import Firefox from selenium.webdriver.common.by import By ...

Matching an element that contains a specific string in JS/CSS

I'm currently faced with an HTML file that's being generated by an outdated system, making it tricky for me to control the code generation process. The structure of the HTML code is as follows: <table cellpadding=10> <tr> ...

Placing text at the bottom of a backdrop image

Looking for a way to position text at the bottom of a background image? Here's how you can do it: <div style="background-image: url('http://www.hdwallpapersinn.com/wp-content/uploads/2013/03/1325120512b60-45927-1.jpg'); height:100px"> ...