title: print files with java using lpr
body: <p>I'm writing an lpr client on <strong>java</strong> to send files (PDF, doc, etc) to a printer (hp laser-jet 4250dtn)
  Both client and server are running <strong>UNIX</strong></p>
  
  <p>The printer is on a server (that's why I'm using lpr) Everything is all right and I indeed managing  to open the socket, sending the control file(according to RFC 1179) and sending the file itself.</p>
  
  <p>the file is getting to the printer "queue".</p>
  
  <p>My problem is when I want to print the file from the printer, it's comes out wrong and not understandable at all (can't see any text just weird symbols).</p>
  
  <p>My question is what file type shout I give or make (change the file to postscript??) in order that the printer will understand me? Or am I doing anything else wrong?</p>
  
  <p>Im attaching the code here so you can see and highlighted the line in which im opening the file from the file-path </p>
  
  <pre><code>Socket socketLpr = getSocket(hostName);
      socketLpr.setSoTimeout(30000);
      OutputStream sOut = socketLpr.getOutputStream();
      InputStream sIn = socketLpr.getInputStream();
  
      //Open printer
      s = "\002" + printerName + "\n";
      sOut.write(s.getBytes());
      sOut.flush();
      acknowledge(sIn, "lpr Failed to open printer");
  
      //Send control file     
      controlFile += "H" + hostName + "\n";
      controlFile += "P" + userName + "\n";
      controlFile += ((printRaw) ? "o":"p") +"dfA" + strJobNumber + hostName + "\n";
      controlFile += "UdfA" + strJobNumber + hostName + "\n";
      controlFile += "N" + documentName + "\n";
  
      s = "\002" + (controlFile.length()) + " cfA" + strJobNumber + hostName + "\n";
      sOut.write(s.getBytes());
  
      acknowledge(sIn, "lpr Failed to send control header");
  
      buffer = controlFile.getBytes();
      sOut.write(buffer);
      buffer[0] = 0;
      sOut.write(buffer, 0, 1);
      sOut.flush();
  
      acknowledge(sIn, "jLpr Failed to send control file");
  
      //Send print file       
      **f = new File(fileName);** // &lt;- here is when im oppening the file(pdf,word,etc)
      if (!(f.exists() &amp;&amp; f.isFile() &amp;&amp; f.canRead())) {
          throw new IOException("jLpr Error opening print file");
      }
      s = "\003" + (f.length()) + " dfA" + strJobNumber + hostName + "\n";
      sOut.write(s.getBytes());
      sOut.flush();
      acknowledge(sIn, "jLpr Failed to send print file command");
  
      FileInputStream fs = new FileInputStream(f);
  
      int readCounter;
      do {
          readCounter = fs.read(buffer);
          if (readCounter&gt;0) {
              sOut.write(buffer, 0, readCounter);
          }
      } while (readCounter&gt;0);
      buffer[0] = 0;
      sOut.write(buffer,0,1);
      sOut.flush();
      acknowledge(sIn, "jLpr Failed to send print file");
  
      socketLpr.close();
  }
  </code></pre>