|
| 1 | +package lu.hrs.mirth; |
| 2 | + |
| 3 | +import java.awt.print.PrinterException; |
| 4 | +import java.io.ByteArrayInputStream; |
| 5 | +import java.io.ByteArrayOutputStream; |
| 6 | +import java.io.DataOutputStream; |
| 7 | +import java.net.Socket; |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Paths; |
| 11 | +import java.util.Base64; |
| 12 | + |
| 13 | +import javax.print.DocFlavor; |
| 14 | +import javax.print.SimpleDoc; |
| 15 | +import javax.print.StreamPrintServiceFactory; |
| 16 | +import javax.print.attribute.HashPrintRequestAttributeSet; |
| 17 | +import javax.print.attribute.PrintRequestAttributeSet; |
| 18 | +import javax.print.attribute.standard.Copies; |
| 19 | +import javax.print.attribute.standard.MediaSizeName; |
| 20 | +import javax.print.attribute.standard.PageRanges; |
| 21 | +import javax.print.attribute.standard.Sides; |
| 22 | + |
| 23 | +import org.apache.pdfbox.pdmodel.PDDocument; |
| 24 | +import org.apache.pdfbox.printing.PDFPrintable; |
| 25 | +import org.apache.pdfbox.printing.Scaling; |
| 26 | + |
| 27 | +import com.itextpdf.html2pdf.HtmlConverter; |
| 28 | + |
| 29 | +/** |
| 30 | + * Prints certain types of documents on a network printer.<br> |
| 31 | + * <br> |
| 32 | + * The specialty about this class is that the printer does not have to have the driver for the desired printers configured on the |
| 33 | + * client system. <br><br> |
| 34 | + * <i><b><u>This Class needs the following libraries:</u></b><br><br> |
| 35 | + * <u>For transforming PDF to postscript:</u><br> |
| 36 | + * pdfbox-2.0.jar<br> |
| 37 | + * fontbox-2.0.jar<br><br> |
| 38 | + * <u>For transforming html to PDF:</u><br> |
| 39 | + * commons-logging-1.1.1.jar<br> |
| 40 | + * html2pdf-2.1.1.jar<br> |
| 41 | + * io-7.1.4.jar<br> |
| 42 | + * kernel-7.1.4.jar<br> |
| 43 | + * layout-7.1.4.jar<br> |
| 44 | + * slf4j-api-1.7.25.jar<br> |
| 45 | + * slf4j-simple-1.7.25.jar<br> |
| 46 | + * styled-xml-parser-7.1.4.jar<br> |
| 47 | + * svg-7.1.4.jar<br> |
| 48 | + * </i><br> |
| 49 | + * (newer versions of the libraries might work as well) |
| 50 | + * |
| 51 | + * @author ortwin.donak |
| 52 | + * |
| 53 | + */ |
| 54 | +public class NetworkPrinting { |
| 55 | + |
| 56 | + /** |
| 57 | + * Sends a text to a network printer using UTF-8 encoding |
| 58 | + * |
| 59 | + * @param text |
| 60 | + * The text that should be printed |
| 61 | + * @param printerIdentifier |
| 62 | + * The ip or qualified name of the printer |
| 63 | + * @throws Exception |
| 64 | + * View exception message for details |
| 65 | + */ |
| 66 | + public static void printText(String text, String printerIdentifier) throws Exception { |
| 67 | + printText(text, null, printerIdentifier); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Sends a text to a network printer |
| 72 | + * |
| 73 | + * @param text |
| 74 | + * The text that should be printed |
| 75 | + * @param encoding |
| 76 | + * The encoding of the text <i>(DEFAULT: "UTF-8")</i> |
| 77 | + * @param printerIdentifier |
| 78 | + * The ip or qualified name of the printer |
| 79 | + * @throws Exception |
| 80 | + * View exception message for details |
| 81 | + */ |
| 82 | + public static void printText(String text, String printerIdentifier, String encoding) throws Exception { |
| 83 | + if (encoding == null) { |
| 84 | + encoding = "URF-8"; |
| 85 | + } |
| 86 | + |
| 87 | + // try to create a connection to the printer |
| 88 | + Socket printer = new Socket(printerIdentifier, 9100); |
| 89 | + // and get a handle to the output stream |
| 90 | + DataOutputStream printerFeed = new DataOutputStream(printer.getOutputStream()); |
| 91 | + printerFeed.write(text.getBytes(encoding)); |
| 92 | + |
| 93 | + // clean up |
| 94 | + printer.close(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Prints an html file on a network printer |
| 99 | + * |
| 100 | + * @param html |
| 101 | + * The self-containing html file (images, etc. must be included) |
| 102 | + * @param printerIdentifier |
| 103 | + * The fully qualified name or ip of the network printer |
| 104 | + * @throws Exception |
| 105 | + * View exception message for details |
| 106 | + */ |
| 107 | + public static void printHtml(String html, String printerIdentifier) throws Exception { |
| 108 | + // convert the html page to pdf |
| 109 | + ByteArrayOutputStream pdf = new ByteArrayOutputStream(); |
| 110 | + HtmlConverter.convertToPdf(new ByteArrayInputStream(html.getBytes(StandardCharsets.UTF_8)), pdf); |
| 111 | + // print the pdf file |
| 112 | + printPdf(pdf.toByteArray(), printerIdentifier); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Prints a base64-encoded document on a network printer. The network printer does not need to be configured at the server.<br> |
| 117 | + * <br> |
| 118 | + * <i>This functionality needs the following Apache libraries: pdfbox-2.0.jar, fontbox-2.0.jar (or higher)</i> |
| 119 | + * |
| 120 | + * @param encodedDocumentToPrint |
| 121 | + * The base64-encoded document that should be printed |
| 122 | + * @param printerIdentifier |
| 123 | + * The name or ip of the network printer to which the document should be sent |
| 124 | + * @throws Exception |
| 125 | + * Mainly if the printer is not known or not accessible, |
| 126 | + */ |
| 127 | + public static void printPdfBase64(String encodedDocumentToPrint, String printerIdentifier) throws Exception { |
| 128 | + printPdf(Base64.getDecoder().decode(encodedDocumentToPrint), printerIdentifier); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Prints a PDF-document on a network printer. The network printer does not need to be configured at the server.<br> |
| 133 | + * <br> |
| 134 | + * <i>This functionality needs the following Apache libraries: pdfbox-2.0.jar, fontbox-2.0.jar (or higher)</i><br> |
| 135 | + * <br> |
| 136 | + * <p style="color:Gainsboro;"> |
| 137 | + * This is necessary as we bypassing the printers driver (as there is none installed on the requesting system). Thus, the function has to take |
| 138 | + * care for transforming the file to postscript and to directly communicate with the printer. For doing so, it hijacks an existing postscript |
| 139 | + * driver of another printer. |
| 140 | + * </p> |
| 141 | + * |
| 142 | + * @param documentToPrint |
| 143 | + * The PDF-document that should be printed |
| 144 | + * @param printerIdentifier |
| 145 | + * The name or ip of the network printer to which the document should be sent |
| 146 | + * @throws Exception |
| 147 | + * Mainly if the printer is not known or not accessible |
| 148 | + */ |
| 149 | + public static void printPdf(byte[] documentToPrint, String printerIdentifier) throws Exception { |
| 150 | + |
| 151 | + // speed the rendering up |
| 152 | + System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider"); |
| 153 | + |
| 154 | + // check for any driver that are capable of postscript printing |
| 155 | + DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; |
| 156 | + StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, |
| 157 | + DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType()); |
| 158 | + // are there any? |
| 159 | + if (factories.length == 0) { |
| 160 | + // nope - sorry but won't work on this system. Install postscript printer driver first... |
| 161 | + throw new PrinterException("No PostScript factories available"); |
| 162 | + } |
| 163 | + |
| 164 | + // try to create a connection to the printer |
| 165 | + Socket printer = new Socket(printerIdentifier, 9100); |
| 166 | + // and get a handle to the output stream |
| 167 | + DataOutputStream printerFeed = new DataOutputStream(printer.getOutputStream()); |
| 168 | + |
| 169 | + // now get the attachment content |
| 170 | + ByteArrayInputStream documentData = new ByteArrayInputStream(documentToPrint); |
| 171 | + // load the pdf document |
| 172 | + PDDocument document = PDDocument.load(documentData); |
| 173 | + documentData.close(); |
| 174 | + |
| 175 | + // Attributes are specified by https://docs.oracle.com/javase/7/docs/api/ |
| 176 | + // see package javax.print.attribute.standard |
| 177 | + PrintRequestAttributeSet printingAttributes = new HashPrintRequestAttributeSet(); |
| 178 | + // set European page size |
| 179 | + printingAttributes.add(MediaSizeName.ISO_A4); |
| 180 | + // printing should be double sided |
| 181 | + printingAttributes.add(Sides.DUPLEX); |
| 182 | + // set number of pages to be printed |
| 183 | + printingAttributes.add(new PageRanges(1, document.getNumberOfPages())); |
| 184 | + // create an implicit print job and print the document to the anonymous network printer |
| 185 | + factories[0].getPrintService(printerFeed).createPrintJob() |
| 186 | + .print(new SimpleDoc(new PDFPrintable(document, Scaling.ACTUAL_SIZE, false), flavor, null), printingAttributes); |
| 187 | + |
| 188 | + // clean up |
| 189 | + printer.close(); |
| 190 | + document.close(); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Prints a document on a network printer |
| 195 | + * |
| 196 | + * @param documentToPrint |
| 197 | + * The document that should be printed |
| 198 | + * @param printerIdentifier |
| 199 | + * The name or ip of the network printer to which the document should be sent |
| 200 | + * @throws Exception |
| 201 | + * Mainly if the printer is not known or not accessible |
| 202 | + */ |
| 203 | + public static void printOnNetworkPrinter(Object documentToPrint, String printerIdentifier) throws Exception { |
| 204 | + |
| 205 | + // check for any driver that are capable of postscript printing |
| 206 | + DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; |
| 207 | + StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, |
| 208 | + DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType()); |
| 209 | + // are there any? |
| 210 | + if (factories.length == 0) { |
| 211 | + // nope - sorry but won't work on this system. Install postscript printer driver first... |
| 212 | + throw new PrinterException("No PostScript factories available"); |
| 213 | + } |
| 214 | + |
| 215 | + // try to create a connection to the printer |
| 216 | + Socket printer = new Socket(printerIdentifier, 9100); |
| 217 | + // and get a handle to the output stream |
| 218 | + DataOutputStream printerFeed = new DataOutputStream(printer.getOutputStream()); |
| 219 | + |
| 220 | + // now get the document content |
| 221 | + ByteArrayInputStream document = new ByteArrayInputStream((byte[]) documentToPrint); |
| 222 | + |
| 223 | + // Attributes are specified by https://docs.oracle.com/javase/7/docs/api/ |
| 224 | + // see package javax.print.attribute.standard |
| 225 | + PrintRequestAttributeSet printingAttributes = new HashPrintRequestAttributeSet(); |
| 226 | + // set European page size |
| 227 | + printingAttributes.add(MediaSizeName.ISO_A4); |
| 228 | + // printing should be double sided |
| 229 | + printingAttributes.add(Sides.DUPLEX); |
| 230 | + // set number of pages to be printed |
| 231 | + // printingAttributes.add(new PageRanges(1, document.getNumberOfPages())); |
| 232 | + // just 1 printout |
| 233 | + printingAttributes.add(new Copies(1)); |
| 234 | + // create an implicit print job and print the document to the anonymous network printer |
| 235 | + factories[0].getPrintService(printerFeed).createPrintJob().print(new SimpleDoc(document, flavor, null), printingAttributes); |
| 236 | + |
| 237 | + // clean up |
| 238 | + printer.close(); |
| 239 | + document.close(); |
| 240 | + } |
| 241 | + |
| 242 | + public static void main(String[] args) throws Exception { |
| 243 | + |
| 244 | + // String printer = "SOME_PRINTER.YOUR.DOMAIN"; (e.g. "192.168.12.10" or "PrinterLab02.someHospital.com") |
| 245 | + |
| 246 | + // The following example loads a pdf from file system |
| 247 | + // byte[] testfile = Files.readAllBytes(Paths.get("c:/temp/PrintTest.pdf")); |
| 248 | + // NetworkPrinting.printPdf(testfile, printer); |
| 249 | + |
| 250 | + // IF you want to do it on the fly, use |
| 251 | + // NetworkPrinting.printPdfBase64(Base64Attachment, printer); |
| 252 | + |
| 253 | + // You can also print an HTML document |
| 254 | + // NetworkPrinting.printHtml(html, printer); |
| 255 | + |
| 256 | + // Or simply text: |
| 257 | + // NetworkPrinting.printText("### THIS IS A TEST ###\n\nTEST TEST TEST", printer); |
| 258 | + } |
| 259 | + |
| 260 | +} |
0 commit comments