Opening a generated PNG or PDF for printing

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Opening a generated PNG or PDF for printing

Bill Stephens
All,

My app needs to generate barcodes for printing. I am testing 2 libraries for this, iText and Barbeque. I've been able to generate both PDF from iText containing a barcode and a Barbeque PNG. Now I need to open then in the browser where I can use the browser print support...

Eventually this will be part of a Web Flow: enter product metadata, generate barcode, print barcode, label product, complete flow (remove generated PNG/PDF)

My initial (dirty) Controller code:

def barcode(String id) {

def patient = Patient.findById(id)
def barcode = patient.patientId
def filename = "pdf/" + barcode + ".pdf"

Document document = new Document(new Rectangle(340, 842));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
PdfContentByte cb = writer.getDirectContent();
Barcode128 code128 = new Barcode128();
code128.setCode(barcode);
document.add(code128.createImageWithBarcode(cb, null, null));
document.close();

//what to do here?

}

def barbeque(String id) {
def patient = Patient.findById(id)
def barcodeId = patient.patientId
def filename = "/Users/username/Projects/TCCP/tccp/pdf/" + barcodeId + ".png"

Barcode barcode = BarcodeFactory.createCode128(barcodeId);
File f = new File(filename);
BarcodeImageHandler.savePNG(barcode, f);
//what to do here?
}

  
Reply | Threaded
Open this post in threaded view
|

Re: Opening a generated PNG or PDF for printing

Daniel Woods
Comments inline:
 

def barcode(String id) {

def patient = Patient.findById(id)
def barcode = patient.patientId
def filename = "pdf/" + barcode + ".pdf"

Document document = new Document(new Rectangle(340, 842));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
PdfContentByte cb = writer.getDirectContent();
Barcode128 code128 = new Barcode128();
code128.setCode(barcode);
document.add(code128.createImageWithBarcode(cb, null, null));
document.close();

//what to do here?

I think the only thing that you can do here is re-open the generated PDF and stream it back to the client:

response.contentType = "application/pdf"
response.outputStream << new File(filename).bytes

 
}

def barbeque(String id) {
def patient = Patient.findById(id)
def barcodeId = patient.patientId
def filename = "/Users/username/Projects/TCCP/tccp/pdf/" + barcodeId + ".png"

Barcode barcode = BarcodeFactory.createCode128(barcodeId);
File f = new File(filename);
BarcodeImageHandler.savePNG(barcode, f);
//what to do here?
}

Similar to above, but you already have the file object here, so:

response.contentType = "image/png"
response.outputStream << f.bytes 

  

 /dan
t: @danveloper

Reply | Threaded
Open this post in threaded view
|

Re: Opening a generated PNG or PDF for printing

Bill Stephens
Both work perfectly.

+10 points for @danveloper! 


On Fri, Dec 27, 2013 at 8:16 AM, Daniel Woods <[hidden email]> wrote:
Comments inline:
 

def barcode(String id) {

def patient = Patient.findById(id)
def barcode = patient.patientId
def filename = "pdf/" + barcode + ".pdf"

Document document = new Document(new Rectangle(340, 842));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
PdfContentByte cb = writer.getDirectContent();
Barcode128 code128 = new Barcode128();
code128.setCode(barcode);
document.add(code128.createImageWithBarcode(cb, null, null));
document.close();

//what to do here?

I think the only thing that you can do here is re-open the generated PDF and stream it back to the client:

response.contentType = "application/pdf"
response.outputStream << new File(filename).bytes

 
}

def barbeque(String id) {
def patient = Patient.findById(id)
def barcodeId = patient.patientId
def filename = "/Users/username/Projects/TCCP/tccp/pdf/" + barcodeId + ".png"

Barcode barcode = BarcodeFactory.createCode128(barcodeId);
File f = new File(filename);
BarcodeImageHandler.savePNG(barcode, f);
//what to do here?
}

Similar to above, but you already have the file object here, so:

response.contentType = "image/png"
response.outputStream << f.bytes 

  

 /dan
t: @danveloper


Reply | Threaded
Open this post in threaded view
|

Re: Opening a generated PNG or PDF for printing

mimimimi
In reply to this post by Bill Stephens
except  Barbeque, you can find many ways to generate barcode and print code 128 from pdf or some other document, such as Zxing barcode library, ID Automation and so on.