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

Hello Monks,

Im back. Well, working with TK and I am trying to figure out a solution to be able to print a page. I tried WIN32::Printer but to no avail, I kept getting error -3 "Unable to connect to printer" I tried every possible solution (I think it might have to do with the fact that all printers are on a network and none are local to me) but anyway, I have since moved onto trying to build an HTML page and convert it to PDF, then linking to it from the program so they can print it.

I am getting this error:

Tk::Error: Can't write to '\CompletedOrder.gif-3128': Permission denie +d at C:/Perl/lib/LWP/ line 105. at C:/Perl/lib/LWP/UserAge line 891. at C:/Perl/site/lib/PDF/ line 141. LWP::UserAgent::mirror at C:/Perl/lib/LWP/ line 891 LWP::Simple::mirror at C:/Perl/lib/LWP/ line 91 PDF::FromHTML::Twig::__ANON__ at C:/Perl/site/lib/PDF/FromHTML/Twig.p +m line 192 XML::Twig::_twig_end at C:/Perl/site/lib/XML/ line 2295 XML::Parser::Expat::parse at C:/Perl/lib/XML/Parser/ line 474 XML::Parser::parse at C:/Perl/lib/XML/ line 192 Carp::croak at C:/Perl/site/lib/ line 100 XML::Twig::_croak at C:/Perl/site/lib/XML/ line 2821 XML::Twig::_checked_parse_result at C:/Perl/site/lib/XML/ line + 766 XML::Twig::parse at C:/Perl/site/lib/XML/ line 747 PDF::FromHTML::parse_file at C:/Perl/site/lib/PDF/ line 14 +1 PDF::FromHTML::convert at C:/Perl/site/lib/PDF/ line 172 main::ViewSingleOrder at C:\BLABAD~1\/ line 244

I am using the following code (Taken from CPans Docs):
my $pdf = PDF::FromHTML->new( encoding => 'utf-8' ); # Loading from a file: # $pdf->load_file('source.html'); # Or from a scalar reference: $pdf->load_file(\$page_html); # Perform the actual conversion: $pdf->convert( # With PDF::API2, font names such as 'traditional' also works Font => 'font.ttf', LineHeight => 10, Landscape => 1, ); # Write to a file: $pdf->write_file('target.pdf');

Thank you

Replies are listed 'Best First'.
by talexb (Canon) on Sep 26, 2012 at 16:25 UTC
      Tk::Error: Can't write to '\CompletedOrder.gif-3128': Permission denied at C:/Perl/lib/LWP/ line 105. at C:/Perl/lib/LWP/ line 891.

    You should probably deal with this first error -- it looks like it's also the easiest one to deal with. Try writing to your Desktop (or somewhere that you own) instead of the root directory of your machine (which is owned by the system).

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

by rpnoble419 (Pilgrim) on Sep 26, 2012 at 17:56 UTC

    I use Win32::Printer everyday on network printers. The trick is to have the printer name exactly match the name of the printer queue.

    Here is sample code to print to the PDF file or the printer

    if ($PrintAsPDF==0) { our $dc = new Win32::Printer( width => $PageWidth, height => $PageHeight, description => 'Sample Description', printer => $PrinterName, orientation => $PageOrientation, unit => $Units); } else { our $dc = new Win32::Printer( width => $PageWidth, height => $PageHeight, description => 'Sample Description', printer => $PrinterName, file => $PDFFileName.'.pdf', pdf => 1, unit => $Units); }

    I usually use the rename function on the printers queue to highlight the printer name and copy it into my application configuration YAML.

    Don't forget to to install Ghostscript or you can't print a PDf file.

    From the Win32::Printer POD

    "Set this attribute if You want to convert PostScript printer drivers output to PDF format. WARNING: This feature needs installed Ghostscript and atleast one PostScript printer driver. Use this attribute with "file" or "prompt" attributes."

by Anonymous Monk on Sep 27, 2012 at 01:29 UTC
    I have found that it is easy to convert from docbook to HTML and from docbook to PDF. This might be worth looking into
by Steve_BZ (Chaplain) on Sep 27, 2012 at 12:35 UTC

    Hi Randor,

    Are going to or from pdf?

    If you are trying to generate a pdf doc, you could try: Wx::PdfPrinter. Admittedly it means using Wx, but I guess you could confine it to a module to:

    1. Create an html object,
    2. Load the html page into the object,
    3. create an wxHtmlEasyPrinting object and pass it to Wx::PdfPrinter.

    Maybe it's overkill for what you want.

    Good luck.