Solved

Page breaks in printing.

Posted on 2014-04-30
9
187 Views
Last Modified: 2014-05-29
What is the easiest way to insert a form feed to a printer?.  I've tried looking on the net but they don't seem to work.
0
Comment
Question by:breeze351
  • 4
  • 3
  • 2
9 Comments
 
LVL 13

Expert Comment

by:AielloJ
ID: 40032820
breeze351,

It's possible, but the preferred way is to create a PDF file of what you want to print.  You have much more control.  If you still want to do it from the HTML, then you first need a CSS style as follows:

single_page{
page-break-after: always;
}

This is a CSS standard and SHOULD work, but different browsers, versions, and other CSS, could affect its reliability, thus the PDF as a preference. Next, output your content using the single page class.

echo '<p class="single_page">Your result here</p>';

Best regards,

AielloJ
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40033414
Agree with AielloJ.  Here is my thinking.

HTML is a semantic markup language, designed to attach meta-meaning to pieces of data.  It was designed to be flexible and accomodative of the window viewport.  It was accidentally pressed into service as a webpage formatting language back in the 1990s (remember <table> tags?) when people did not understand or use CSS very effectively.  Those days are long since gone.  Make a Google search for "CSS ZEN Garden" to see how the same HTML can use different CSS to achieve different appearances.

PDF is a document layout language.  It has no understanding of semantic meaning and facilitates detailed and rigid placement of data on the printed page.  CSS means nothing to PDF.

HTML and PDF are not quite as different as fish and bicycles, but nearly so.  For viewing data on a "device" HTML5 is the right choice, no matter what the device might be.  But for printing, PDF is the right choice.  You can think of these as two different "views" in the Model-View-Controller design pattern.

There are two free and open source PDF libraries that I've used - FPDF and TCPDF.  Both work well and are amazingly fast and accurate.  Both require some time to set up the exact document layout (data elements can be positioned to within 1/100 inch).  Once set up, it's easy to produce great-looking documents for print on almost any media, from business cards to banner-width posters.

To give you an idea of what you're in for, here is my "hello world" exercise using FPDF.

<?php // demo/fpdf_hello_world.php
error_reporting(E_ALL);


// DEMONSTRATE SOME OF THE BASICS OF FPDF


// SOME VARIABLES FOR OUR TESTS (COULD COME FROM DATA BASE, ETC)
$font = 'Arial';
$text = 'Hello World!';

// BRING IN THE PDF THING
require_once('fpdf16/fpdf.php');

// SYNTHESIZE THE PDF FILE INFORMATION
$pdf_file_link
= DIRECTORY_SEPARATOR
. 'RAY_junk'
. DIRECTORY_SEPARATOR
. 'temp_pdf_blue'
. '.pdf'
;
$pdf_file_name
= getcwd()
. $pdf_file_link
;

// DO THE HELLO WORLD EXERCISE IN WHITE ON A BLUE BACKGROUND
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont($font, 'B', 16);
$pdf->SetFillColor(  0,   0, 255);
$pdf->SetTextColor(255, 255, 255);
$pdf->Cell(40, 10, $text, 0, 2, 'L', TRUE);

// WRITE THE PDF TO DISK
$pdf->Output($pdf_file_name, 'F');

// PRESENT A LINK
echo '<a target="my_PDF" href="' . $pdf_file_link . '">Blue PDF</a>';



// START OVER FOR A NEW PDF
unset($pdf);
echo PHP_EOL . "<br/>";

// SYNTHESIZE THE PDF FILE INFORMATION
$pdf_file_link
= DIRECTORY_SEPARATOR
. 'RAY_junk'
. DIRECTORY_SEPARATOR
. 'temp_pdf_red'
. '.pdf'
;
$pdf_file_name
= getcwd()
. $pdf_file_link
;

// DO THE HELLO WORLD EXERCISE IN WHITE ON A RED BACKGROUND
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont($font, 'B', 16);
$pdf->SetFillColor(255,   0,   0);
$pdf->SetTextColor(255, 255, 255);
$pdf->Cell(40, 10, $text, 0, 2, 'L', TRUE);

// WRITE THE PDF TO DISK
$pdf->Output($pdf_file_name, 'F');

// PRESENT A LINK
echo '<a target="my_PDF" href="' . $pdf_file_link . '">Red PDF</a>';

Open in new window

HTH, ~Ray
0
 

Author Comment

by:breeze351
ID: 40035163
If I understand all the reply.

Create a pdf and then send it to the local printer?

I thought it would be easier to just send it to the printer.

Your solution will probably solve another problem that I have deal with.  Save the print as a pdf.

Glenn
0
 

Author Comment

by:breeze351
ID: 40035185
One more question.
Once the pdf is created, how does it get thrown to the printer?
I didn't see it in the code.
Glenn
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40035424
When you run that script, it creates the PDF in a file on the server and presents a link to the client.  When you click the link, your browser will open the PDF, and from there you can print it.  I do not know of any way a server-side script can start a printer on a client machine -- I think that has to be handled by the client (it's a security consideration).
0
 

Author Comment

by:breeze351
ID: 40038054
The css didn't work.
I added to the css:

single_page {
page-break-after: always;
      }

I changed the print inclusion to follow:

// Iterate through data and populate
$count = 0;
echo '<p class="single_page">';
while ($ROW = Mysql_fetch_assoc($Survey_Data, MYSQL_BOTH))
      {
            $count = ++$count;
            // Logo
            echo '<table width = "1280px">';
            echo '<tr>'

etc,etc..................

echo '<table width="100%" border="1">';
                  echo '<td> Errrors And Omissions <td>';
                  echo '</tr>';
                  echo '</table>';
                  echo '</p>';
            }

Did I miss something.  The page prints correctly, but I'm not getting the form feed.

Glenn
0
 
LVL 13

Expert Comment

by:AielloJ
ID: 40050969
Glenn,

I'm not sure if wrapping a table in the <p> tag will work as you want it.  Try two simple paragraphs and see if that works.  If it does, then my doubts about wrapping the table are correct.

Take a minute to cut and paste Ray's code and try it out.  Most of what's there is 'boilerplate' needed for creating any pdf, and the place to put your text is on line 10.  In other words, the you can give it a try by changing line 10 only, to demonstrate how easy this better solution is.   Ray's examples are usually spot on and correct.  I think once you try it, you'll be hooked.

Best regards,

AielloJ
0
 

Author Comment

by:breeze351
ID: 40068375
Guys:
Here is also something that I've seen and it does work.

My partner is running Win 8 and IE 10.  Across the top of the menu bar, you can create a PDF file, print something and add to the same PDF file.

This would solve allot of my problems with out having to do any coding with FPDF.  I did look at it and I'm going to remember it and use it for other applications.

 My problem right now is I need to put on a dog and pony show next week.  I have the print formats they want, but
 I need to figure out how to put a page break in a straight print to the local printer.   I know the number of lines and, what will fit on a page and where the breaks should be.

 Any ideas on the page break?

Thanks
Glenn
0
 
LVL 13

Accepted Solution

by:
AielloJ earned 500 total points
ID: 40077978
Glenn,

Does the demo have to run 100% from the browser?  The easiest thing to do right now is to make the PDF file, then manually edit in the page break.  If that won't work for the demo, you may want to split up the page display into separate pages, with a 'next' button on them, then use the PDF print to append each page to the bottom of the PDF file.

What you're running into is what I warned about in my initial post.  There's so many things in a web page that can affect how well something like this works.  If you look at most web pages that print multiple pages, they use the 'next' link strategy between pages.

AielloJ
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

This is a how to build your own CSS3 slideshow and when I say CSS3 I mean just CSS with no javascript in sight! There a few examples online of how to do this but most just show you an example without any explanation, others make it more complicat…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now