9

I have to print a pdf file using a printer with a specific IP address. I am able to print a specific text but I want to print a file or a html parsed text.

My Code:

try {
    Socket sock = new Socket("192.168.0.131", 9100);
    PrintWriter oStream = new PrintWriter(sock.getOutputStream());
    oStream.println("HI,test from Android Device");
    oStream.println("\n\n\n");
    oStream.close();
    sock.close();
} catch (UnknownHostException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Also please give the reason for down voting

Edit

Many people are suggesting about PDLs but how to convert the pdf to PDL?

Vishwajit Palankar
  • 2,772
  • 2
  • 24
  • 44

3 Answers3

3

Data sent to a printer must be in Page Description Languages(PDL's), languages printer understand. Here you can find some basic understanding o those. ASCII string is understood by most of the printers, that is why you are able to print the string. But when it comes to complex document formats like(PDF's, Excels, HTML page, etc), you have to convert the document into one of the PDLs. The most common PDLs that i have worked with are PostScript(PS) and PCL(Printer Command Language).

Now in order to print a PDF with exact formatting(to which you require a solution), you have to convert the PDF document to PCl or Postscript and then send that PCL or postscript data to the printer. You can use ghostscript to convert your PDF to PS or PCL.

I have not done exactly what you are trying to do, but i think what i have explained above is the start for you.

I would be highly interested to know if you are able to do it. Let me know.

Zaartha
  • 1,065
  • 9
  • 24
  • I googled a lot but i am not able to find any library to convert the pdf to PDL, so if you know any please share the links – Vishwajit Palankar Oct 12 '16 at 14:53
  • @VishwajitPalankar I am afraid there is no java/android library that does it. I used ghostscript compiled for android to convert ps to pdf. You can do the same and convert pdf to ps. Here is link to question. Look at the answer of chrisl http://stackoverflow.com/questions/37478507/cross-compiling-ghostscript-for-android-what-host-should-i-use . Use this answer to build ghostscript for android. In order to make this work i have to root my device and its a long process – Zaartha Oct 12 '16 at 19:10
  • @VishwajitPalankar The easier way would be to upload your pdf file to a server, then convert it to PS or PCL using ghostscript for linux and then using that PS file. There will always be be a network lag. Let me know what you do. – Zaartha Oct 12 '16 at 19:13
  • help me out here how to convert pdf to raw data which is printer understandable – Vishwajit Palankar Oct 15 '16 at 10:17
2

You need to use PDFBox library which is also available for Android.

You can use it to get the PDF text and then use it for your purpose -

A java sample -

import java.io.File;
import java.io.IOException; 
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.PDFTextStripperByArea;

public class myProgram{

    public static void main(String[] args)
    try {
        PDDocument document = null;
        document = PDDocument.load(new File("my_file.pdf"));
        document.getClass();
        if (!document.isEncrypted()) {
        PDFTextStripperByArea stripper = new PDFTextStripperByArea();
        stripper.setSortByPosition(true);
        PDFTextStripper Tstripper = new PDFTextStripper();
        String st = Tstripper.getText(document);
        System.out.println("Text:" + st);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

PdfBox-For-Android

Or use MuPDF

Techidiot
  • 1,851
  • 1
  • 11
  • 27
  • this library is good for reading the text of the pdf but i want to print the pdf as it is like the tables and all that are shown using this library i am only able to print the texts from the pdf not the actual pdf – Vishwajit Palankar Sep 21 '16 at 13:21
  • http://stackoverflow.com/questions/3203790/parsing-pdf-files-especially-with-tables-with-pdfbox – Techidiot Sep 28 '16 at 04:18
0

As suggested above, you can try pdfbox for extracting text but you also need to preserve formatting specially, around tables for which pdftron seems to be a better choice. It offers lots of convenient apis to achieve the same. You can refer to their site for fully working examples.

Note: I am not representing pdftron nor I work for them.

Prateek Jain
  • 1,973
  • 2
  • 23
  • 37