Connecting Tech Pros Worldwide Help | Site Map

Absolute positioned HTML elements not printing

Member
 
Join Date: Jan 2009
Posts: 33
#1: Apr 7 '09
I am having an issue with HTML elements not printing when positioned absolutely when they extend beyond the first page. I am working on some foreign tax refund forms. The forms are cut into multiple jpg images, so that I can position them on the HTML page, so that they print properly (page1, page2, etc). The information to complete the forms is then positioned using position: absolute and a z-index of +1.

Here is an example code of my problem:
(*note: I am using Mac OS X 10.5.6 and Firefox 3.0.8)

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <div id="1" STYLE="position: absolute; top: 0px; left: 0px;">test1</div>
  4. <div id="2" STYLE="position: absolute; top: 1000px; left: 0px;">test2</div>
  5. </body>
  6. </html>
  7.  
Everything displays fine in the browser, but when I go to print, anything past page one, that is aligned using absolute, will not print. I have tried saving directly to .pdf as well (since I have a mac), changed the print settings to include background images and colors (stab in the dark), and have tried using relative and static, but then the images that are the form do not show properly (they get pushed down the page).

If you need more code, let me know. Also, the reason I am using inline css is because these divs are actually created on the fly, and trying to create a stylesheet to apply to each div(since the data is always going to be changing) will not work.
Member
 
Join Date: Jan 2009
Posts: 33
#2: Apr 7 '09

re: Absolute positioned HTML elements not printing


Alright, so it gets more interesting.

I've been trying to get the printing to work, and tried setting up the absolute positioned element within a relative positioned element (since an absolute positioned element is based on its most recent ancestor with positioning applied)

Here was my first test:

Expand|Select|Wrap|Line Numbers
  1.  <html>
  2.  <body>
  3.  <div id="1" STYLE="position: absolute; top: 0px; left: 0px;">test1</div>
  4.  
  5. <div id="3" STYLE="position: relative; margin-top: 1000px;">test3
  6.  <div id="2" STYLE="position: absolute; top: 10px; left: 0px;">test2</div>
  7. </div>
  8. </body>
  9. </html>
  10.  
It would print a blank second page.

So, I tried this:

Expand|Select|Wrap|Line Numbers
  1.  <html>
  2.  <body>
  3.  <div id="1" STYLE="position: absolute; top: 0px; left: 0px;">test1</div>
  4. <div id="4" STYLE="margin-top: 1000px;">test4</div>
  5. <div id="3" STYLE="position: relative; margin-top: 1000px;">test3
  6.  <div id="2" STYLE="position: absolute; top: 10px; left: 0px;">test2</div>
  7. </div>
  8. </body>
  9. </html>
  10.  
This kind of works. It displays in the browser really oddly, but it will print two pages, with test2 and test 3 at the top of the second page....

Still don't know why, and I would like to so that I can get the browser to display it properly AND the page to print properly...maybe I should use two different style sheets, one for screen and one for print? Then I just wouldn't display the <div id="4"> when its on screen, but its there for print?
Member
 
Join Date: Jan 2009
Posts: 33
#3: Apr 7 '09

re: Absolute positioned HTML elements not printing


The solution is actually pretty simple (kicking myself).

Since I am using images, I am placing a div around each image that needs to be displayed to create the form. The div is positioned using position: relative. So, both the image AND the information are placed within the div. Then, the absolute positioned information is adjusted based on the div it is now contained in, and prints as well.

Moral of the story:

Placing an HTML element with absolute position relative to the top of the page will not translate correctly into page breaks (at least with Mac OS X 10.5.6 and Firefox 3.0.8), so, place HTML elements with absolute position within a relatively placed HTML element.

Thanks if you read all of this. Hope it helps someone else.
Newbie
 
Join Date: Apr 2009
Posts: 2
#4: Apr 29 '09

re: Absolute positioned HTML elements not printing


Interesting thread. I've got a similar problem.

I've got a web page in which I want to display images that are precisely positioned on a number of pages. Here's the code:
Expand|Select|Wrap|Line Numbers
  1. <html lang="en" dir="ltr" >
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  4. <style type="text/css">
  5. @page { size: portrait; }
  6. @media all
  7. {
  8.    .page {
  9.       position: relative;
  10.       page-break-after: always; }
  11.  
  12.    .finalpage { position: relative; }
  13. }
  14. </style>
  15.  
  16. </head>
  17. <body>
  18. <div class="page" style="top:0px;">
  19. <div style="position:absolute; left:500px; top:0px;">
  20. <img src="img1.jpg" /></div>
  21. <div style="position:absolute; left:0px; top:0px;">
  22. <img src="img2.jpg" /></div>
  23. <div style="position:absolute; left:50px; top:400px;">
  24. <img src="img3.jpg" /></div>
  25. </div> <!-- closes the 1st page -->
  26. <div class="page" style="top:1414px;">
  27. <div style="position:absolute; left:250px; top:300px;">
  28. <img src="img4.jpg" /></div>
  29. </div>  <!-- closes the 2nd page -->
  30. </body></html>
What should happen is the web page should show img1 near the top-right of page 1, img2 on the top-left of page 1, img3 around in the middle of page 1, and img4 on page 2.

What actually happens is that the images appear where expected on screen, but in Print Preview only the 1st page is rendered: the 2nd page doesn't appear. I'm using Firefox 3.0.10 on Windows XP.

Since each image is precisely placed within its page, each page div needs to have "position:absolute" or "position:relative". I chose relative. One of my CSS rules should force a page break after the 1st page.

I've trawled the web but with no luck. Does anyone have any ideas?
Newbie
 
Join Date: Apr 2009
Posts: 2
#5: Apr 29 '09

re: Absolute positioned HTML elements not printing


I found this (the very last post is the interesting bit). Anyone else have this problem?
http://www.codingforums.com/archive/...p/t-24135.html
Newbie
 
Join Date: Sep 2009
Posts: 2
#6: Sep 24 '09

re: Absolute positioned HTML elements not printing


I have the same problem and I canīt solve the problem yet... Any suggestion?
drhowarddrfine's Avatar
Expert
 
Join Date: Sep 2006
Posts: 5,561
#7: Sep 24 '09

re: Absolute positioned HTML elements not printing


I wish I could be helpful but haven't dealt with this in a long time. The thing to remember is what you see on a web page is not always what you'll get on the printed page, particularly when it crosses page boundaries.

I'm sure I have some articles and/or links about that but I'd have to look them up.
Newbie
 
Join Date: Sep 2009
Posts: 2
#8: Sep 25 '09

re: Absolute positioned HTML elements not printing


I have "solved" my problem... I had to use a div with position relative and inside it I have put my real page... In this div, at the first page the margin-top=0 and in the others I have put margin-top=900, but this wasnīt enough to solve the problem, so I put a <br> inside the div, and then the problem was solved... The screen is exactly the same as the printed page!
Reply