Autoprinting a PDF from a Broswer
June 16, 2014 7:35 AM   Subscribe

I'm working with a cloud-based application (NetSuite) where we need to quickly print a document from the broswer. Right now it's taking too many clicks to get the job done. I'm wondering if there are any scripting/plug-in options we can use to create an auto-print solution.

We're moving to a new system at work that is all browser based. When it's time to create a packing slip for one of our shipments, the worker must select "Save & Print." This launches a separate window where Acrobat loads in the browser. The worker must select Print again from the Acrobat screen, wait for the print job to complete, then close the acrobat window. This is taking too long with too many clicks.

I would like to get the browser to behave so that once the "Save & Print" button is clicked, the second window opens, initiates the print job and closes automatically without the user having to do anything else.

If something like this is possible, how would I go about implementing it? I don't personally have any type of scripting skills, but have a developer at my disposal. I would also be willing to pay an outside person to help me with this task. Thanks!
posted by slogger to Computers & Internet (3 answers total) 1 user marked this as a favorite
 
Are you comfortable using command line tools in Terminal (Mac) or the command prompt (Windows)? You can use wkhtmltopdf with the address of the website (internal or external) that you need to convert to PDF.

Note that the quality of this tool is mixed, please run it on a variety of documents and see if you're happy with its quality!
posted by asymptotic at 7:38 AM on June 16, 2014


I love questions like these!

This is an expensive answer, but I think it will solve your problem quickly if you have .NET (or Java) available to you. I work on a PDF SDK that can add auto-printing into an existing PDF with a few lines of C#:

public static void SelfPrintPdf(Stream istm, Stream ostm)
{
    PdfDocument doc = new PdfDocument(istm);
    doc.SelfPrintingPdf = true;
    doc.Save(ostm);
}
or in Java:
public static void selfPrintPdf(ImageInputStream istm, ImageOutputStream ostm) throws IOException
{
    PdfDocument doc = new PdfDocument(istm);
    doc.setSelfPrintingPdf(true);
    doc.save(ostm);
}


Now, this is just delicious sugar to make the process easier for my users. If you can't use our toolkit but have some other tool or can modify your PDF generation process, you will need to do the following (and I have to be vague about structures because I don't know what tool your using to make the PDF - but this is all in the PDF spec) towards the end of file creation:
  1. if the document catalog doesn't have a Names entry, it will need one
  2. if the Names entry doesn't have a JavaScript entry, add one.
  3. Create an string that contains the following: "this.print({bUI:true,bSilent:false,bShrinkToFit:true});"
  4. add a JavaScript action dictionary to the JavaScript name tree keyed off anything arbitrary you like
  5. write the file
If you're using something like iText or iTextSharp, the preceding is doable with PdfWriter.addJavaScriptAction. At least I think it is - I have an admittedly snobby attitude towards PDF API design and I don't particularly like iText as it really demands that you know the PDF specification to do nearly anything. This is problematic in that it's also easy to create non-compliant documents, which are the bane of my existence.
posted by plinth at 8:26 AM on June 16, 2014


Oh, I should also add (for posterity), that at document open, a compliant PDF viewer, if scripting is enabled, will execute all scripts in the JavaScript action name tree prior to doing most anything else. This is supposed to be a place where you can define methods and other things that are available for other scripts within the document to use, but by putting this.print() into that tree, you require the viewer to print the document at open.

You could get a similar effect by putting the JavaScript action dictionary into the OpenAction entry in the catalog.
posted by plinth at 8:45 AM on June 16, 2014


« Older Teaching/Refugee Education Jobs in Seattle, WA?   |   Collective protective instinct? Newer »
This thread is closed to new comments.