简体   繁体   English

使用PrintWriter通过Socket连接将PDF文件打印到打印机

[英]Print a PDF file to a printer using PrintWriter via Socket connection

I have to print a pdf file using a printer with a specific IP address. 我必须使用具有特定IP地址的打印机打印pdf文件。 I am able to print a specific text but I want to print a file or a html parsed text. 我能够打印特定的文本,但我想打印文件或HTML解析文本。

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? 很多人都建议PDL,但如何将pdf转换为PDL?

Data sent to a printer must be in Page Description Languages(PDL's), languages printer understand. 发送到打印机的数据必须是页面描述语言(PDL),语言打印机才能理解。 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. 大多数打印机都能理解ASCII字符串,这就是您能够打印字符串的原因。 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. 但是当谈到复杂的文档格式(如PDF,Excel,HTML页面等)时,您必须将文档转换为其中一个PDL。 The most common PDLs that i have worked with are PostScript(PS) and PCL(Printer Command Language). 我使用过的最常见的PDL是PostScript(PS)和PCL(打印机命令语言)。

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. 现在,为了打印具有精确格式的PDF(您需要解决方案),您必须将PDF文档转换为PCl或Postscript,然后将该PCL或postscript数据发送到打印机。 You can use ghostscript to convert your PDF to PS or PCL. 您可以使用ghostscript将PDF转换为PS或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. 让我知道。

You need to use PDFBox library which is also available for Android. 您需要使用也适用于Android的PDFBox库。

You can use it to get the PDF text and then use it for your purpose - 您可以使用它来获取PDF文本,然后将其用于您的目的 -

A java sample - 一个java样本 -

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 PDFBOX-FOR-的Android

Or use MuPDF 或者使用MuPDF

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. 如上所述,您可以尝试使用pdfbox提取文本,但您还需要特别保留格式,围绕表格,pdftron似乎是更好的选择。 It offers lots of convenient apis to achieve the same. 它提供了很多方便的apis来实现同样的目标。 You can refer to their site for fully working examples. 您可以参考他们的网站以获取完整的工作示例。

Note: I am not representing pdftron nor I work for them. 注意:我不是代表pdftron,也不是为他们工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM