Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Internet Printing

by arrow (Friar)
on Sep 02, 2003 at 04:00 UTC ( #288235=perlquestion: print w/replies, xml ) Need Help??

arrow has asked for the wisdom of the Perl Monks concerning the following question:

I have a website that uses perl through CGI to make reports. My problem is that i want to print these reports nicely, not through the browser interface, but all the printing modules that i've seen work directly with a printer, not over the internet. I was looking for something that would work off of a "Click here for printer-friendly version" link (or something like that) and than print out a nicely formatted version of the data on screen.
Oh, and i suppose i should have mentioned this earlier, but the host my site is on is not mine, so i don't have control over which modules can be installed. Though maybe i can use my *social engineering skillz to get what i want, but i digress...i've looked pretty extensively through CPAN, but haven't found anything fitting my needs (though perhaps i wasn't looking hard enough). Anyway, any assistance would be greatly appreciated...

Just Another Perl Wannabe

Replies are listed 'Best First'.
Re: Internet Printing
by Zaxo (Archbishop) on Sep 02, 2003 at 04:09 UTC

    Most 'printer-friendy' versions are just rewritings without sidebars, navigation menus, advertising, and so on. You can get that with alternate stylesheets or different templates, for example. Generally, you will not be able to seize the user's printer for a more direct approach.

    After Compline,
    Zaxo

Re: Internet Printing
by BrowserUk (Pope) on Sep 02, 2003 at 05:19 UTC

    Even if you could gain direct access to your users printer, writing your code to handle the 1/2 a zillion different printers on the market, handle the selection of paper sizes available A3, A4, A5, foolscape etc. Color or b&w, draft mode, letter quality, photo-realistic, margins, headers and footers.

    Not to mention that whilst you might think that your data look great in 14pt Ariel, that font might mot be available for the printer in question, and besides, I prefer much smaller fonts. Others may prefer (or require) to use much bigger fonts. You might think it important to have your creative, 1/4 page logo appear on every sheet, but you wouldn't many thanks from me if you did this:)

    Offer a "printer friendly icon" which presents the data as cleanly and simply as possible Preferably without using tables for positioning, or at least without artificially limiting the width of the overall tables or individual columns. That way, If I choose to print in landscape on A3 paper so that 'wrapped' contents of table columns gets printed onto single lines, I can do so.

    For the rest, save yourself time and your users frustrations by letting the browser, the printer driver and the user decide how they want the data printed, rather than trying to do it your way.

    Actually, that is pretty good advice for presenting web pages too.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: Internet Printing
by moxliukas (Curate) on Sep 02, 2003 at 06:59 UTC

    Most of the "printer-friendly" page versions are just differently formatted versions of the webpage. These days, you can use CSS and you don't even need to provide a different version of the webpage. CSS Media Types can be used to assign different stylesheets to different devices, so you can make the navigation bar to be invisible by something like @media print { .navbar { display: none; } } and so on.

Re: Internet Printing
by jonnyfolk (Vicar) on Sep 02, 2003 at 04:42 UTC

    so i don't have control over which modules can be installed.

    Simply create a folder on your server and place your module in that folder. You then refer your script to that folder:

    use lib '/home/users/my_site.com/lib'; use HTML::blah;

    NB. So this would be a file called blah.pm inside a folder called 'HTML' inside a folder called 'lib'.

    As for the main problem I was wondering if you might create a PDF file with your nicely formatted data, then invite the user to print that. I've never done it but there's quite a bit of stuff for PDF on CPAN which might help your cause.

Re: Internet Printing
by BUU (Prior) on Sep 02, 2003 at 04:11 UTC
    Are you looking for a module that you install on your server that automatically opens a client's printer and prints to it? In which case I can tell you that, aside from a few microsoft bugs/worms, nothing like that exists and it would be horrible to even conteplate it. If you just want a page formatted nicely without your menu bars and so forth on it, well, you'll just have to change how you display your data.
Re: Internet Printing
by tachyon (Chancellor) on Sep 02, 2003 at 10:19 UTC

    No doubt I will get slain but the solution is javascript. Note the use of HTML::Template. Every page you want printer friendly should use this basic template. Just insert the content between the DIV tags and the printer friendly script will extract it, add whatever stylesheet you want and pop a new window with a click here to print me link.

    <html> <head> <script> function PrinterFriendlyPage() { var HTML; eval("HTML = document.getElementById('contentStart').innerHTML"); if ( ! HTML || ! window.print ) { alert('Your Browser does not support printing.'); } else { var pfw=window.open("","","toolbar=no,location=no,directories=no,me +nubar=yes,scrollbars=yes,width=750,height=600,left=100,top=25"); pfw.document.open(); pfw.document.write('<html><link rel="stylesheet" type="text/css +" href="docs.css">\n'); pfw.document.write('<'+'script>function printIt () { window.pri +nt() }<'+'/script>\n'); pfw.document.write('<p><a href="javascript:printIt()">Print Thi +s Page</a>\n</p>'); pfw.document.write(HTML); pfw.document.write('</body></html>'); pfw.document.close(); pfw.focus(); } } </script> </head> <body> <!-- All your stardard template stuff, plus the printer friendly link +--> <h1>I am stuff in the template, that is not PF</h1> <a href="javascript:PrinterFriendlyPage()">Printer Friendly Version</a +> <!-- The printer friendly script grabs this DIVs HTML --> <!-- So you want your basic content here of course --> <div id="contentStart"> <h1>Pretend Content</h1> <TMPL_VAR NAME=CONTENT> </div> <!-- More Template stuff --> <h1>More Un-PF stuff</h1> </body> </html>

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Internet Printing
by nite_man (Deacon) on Sep 02, 2003 at 15:19 UTC

    I'd suggest you to convert your HTML report into PDF and then print it via PDF plugin which exists for all popular browsers.

    For example, I use HTMLDOC:

    my $html = 'your html page ...'; my $fname = 'my_report.pdf'; my $flogo = 'your_logo.jpg'; my $backimg = 'your_background.jpg'; open(WRITE, "|htmldoc -t pdf -f $fname --fontsize 10 --header ... --f +ooter t/D --logoimage $flogo --bodyimage $backimg --webpage --size A4 + --top 10mm --bottom 10mm -") or die "Couldn't open htmldoc: $! $?"; print WRITE $html; close WRITE or warn "Some error was generated in the pipe. Error :$! " +;
    In this case, we create PDF file in the webserver directory and then we can retrive it using directly link on this file or mod_perl features.

    It's just example, maybe not good, but it works. I make print view of invoices and payments in my system by this way and then I print those document via internet using PDF plugin.

    Also, try to look at module HTML::HTMLDoc , which implements an Perl interface to the programm HTMLDOC.

    I hope I helped.

    _ _ _ _ _ _
      M i c h a e l

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://288235]
Approved by Zaxo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2020-07-07 07:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?