Generate a PDF That Automatically Prints

Generate a PDF that automatically prints

Method 1: Using embedded javascript inside your PDF files
You can try creating an iText PDFAction object with a javascript call this.print(false) (you can use new PdfAction(PdfAction.PRINTDIALOG) for this), and associate it with the OpenAction event of your pdf file.

The code in iText Java should look like this:

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("file.pdf"));
...
PdfAction action = new PdfAction(PdfAction.PRINTDIALOG);
writer.setOpenAction(action);
...

It should not be too diferent in C#.

As a side note, this is also possible with Amyuni PDF Creator .Net by setting the attribute "AutoPrint" to TRUE in the document class (usual disclaimer applies).

acPDFCreatorLib.Initialize();
acPDFCreatorLib.SetLicenseKey("Amyuni Tech.", "07EFCDA00...BC4FB9CFD");
Amyuni.PDFCreator.IacDocument document = pdfCreator1.Document;

// Open a PDF document from file
System.IO.FileStream file1 = new System.IO.FileStream("test_input.pdf", FileMode.Open, FileAccess.Read, FileShare.Read);

IacDocument document = new IacDocument(null);

if (document.Open(file1, ""))
{
//Set AutoPrint
document.Attribute("AutoPrint").Value = true;

//Save the document
System.IO.FileStream file2 = new System.IO.FileStream("test_output.pdf", System.IO.FileMode.Create, System.IO.FileAccess.Write);
document.Save(file2, Amyuni.PDFCreator.IacFileSaveOption.acFileSaveView);
}

// Disposing the document before closing the stream clears out any data structures used by the Document object
document.Dispose();

file1.Close();

// terminate library to free resources
acPDFCreatorLib.Terminate();

This approach requires the PDF file to be opened in a reader that will take care of printing, and it has the drawback that if the file is saved locally, every time the file is opened later on it will show the print dialog.

Method 2: Using javascript from the browser to communicate with the reader that shows the file.

I found this other approach in this SO question that might worth trying:

<html>
<script language="javascript">
timerID = setTimeout("exPDF.print();", 1000);
</script>
<body>
<object id="exPDF" type="application/pdf" data="111.pdf" width="100%" height="500"/>
</body>
</html>

The idea is to use javascript in the browser to instruct the PDF reader to print the file. This approach will work on PDF files embedded in a HTML page.

Class or function to automatically generate PDF and Print

to create PDFs, theres the great FPDF-library.

printing automatically fortunately isn't possible - just imagine this would be possible and every f***g website could (in addition to the annoying popups and stuff) print out something (advertisements most of the time) on your printer.

EDIT :
if you have control over the clients, you could write a little batch-script like (not tested)

AcroRd32.exe /t %1 printername

and then set pdf-files in your browser to open automatically with this "programm" wich should then print the file without a print-dialogue.

note that you need access to the clients for this and it isn't tested. in theory this works: i did something very similar once to print out labels directly from the browser, but this was a few years ago using WinXP, don't know if this still works on Win7 (or whatever you're using).

generate PDF from window.print() result

You may try to use the download attribute inside your <a> tag:

<a href="content/yourfile.pdf" download > pdf link of your choice </a>
<a href="content/yourfile.doc" download > doc link of your choice </a>

RoR render PDF and print it automatically

So here is my solution:

A new plain HTML-File (create.html.erb) with the following code:

<html>
<head>

<%= javascript_include_tag "http://code.jquery.com/jquery-1.10.0.min.js" %>
<%= javascript_include_tag "http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" %>

<script>
$(document).ready(function() {
pdf = document.getElementById('pdf_source');
pdf.focus();

setTimeout(function(){
pdf.contentWindow.print();
}, 3000);

});
</script>

</head>
<body>

<% url = "#{request.protocol}#{request.host_with_port}/admin/printing_manufacturer_materials_stock?file=#{@file_name}" %>
<iframe id ="pdf_source" src="<%=url %>" style="width:700px;height:500px;"></iframe>

</body>
</html>

In the controller i created the PDF and saved it. The printing-method is sending the saved file (send_file) depending on the param :file.

How to print generated PDF automatically with Wicked PDF?

Actually, print_media_type option is used for styling your pdf file, not for printing it.

To send pdf file directly to printer you can try following:

  1. Generate pdf and save it to a file:

    # create a pdf
    pdf = render_to_string pdf: "some_file_name", template: "templates/pdf", encoding: "UTF-8"

    # then save to a file
    save_path = Rails.root.join('pdfs','filename.pdf')
    File.open(save_path, 'wb') do |file|
    file << pdf
    end
  2. Call lpr command to print saved pdf file

    system("lpr", "pdfs/filename.pdf")

How to print PDFs automatically

This code only works if the printer supports PDF. Otherwise you need to use a native printer or a Java library. There is a blog article on this at http://pdf.jpedal.org/java-pdf-blog/bid/25566/Printing-PDF-files-from-Java



Related Topics



Leave a reply



Submit