使用apache PDFBox jar时出现错误。似乎它以某种方式正在不存在的AFMParser类上调用方法。 AFAIK我已经正确包含了fontbox。 pdfbox,fontbox,commons和gson都在我的构建路径中。

我猜该错误一定与引用有关,但这是我的第一个JAVA项目,没有真正的线索进一步调试它。如果有人有想法,请提出建议。

错误日志:

2018-03-16 14:26:50.020 java[1898:217675] java.lang.NoSuchMethodError: org.apache.fontbox.afm.AFMParser.parse()V
    at org.apache.pdfbox.pdmodel.font.PDFont.addAdobeFontMetric(PDFont.java:166)
    at org.apache.pdfbox.pdmodel.font.PDFont.addAdobeFontMetric(PDFont.java:152)
    at org.apache.pdfbox.pdmodel.font.PDFont.getAdobeFontMetrics(PDFont.java:122)
    at org.apache.pdfbox.pdmodel.font.PDFont.<clinit>(PDFont.java:114)
    at org.apache.pdfbox.pdmodel.font.PDFontFactory.createFont(PDFontFactory.java:108)
    at org.apache.pdfbox.pdmodel.PDResources.getFonts(PDResources.java:213)
    at org.apache.pdfbox.util.PDFStreamEngine.getFonts(PDFStreamEngine.java:612)
    at org.apache.pdfbox.util.operator.SetTextFont.process(SetTextFont.java:69)
    at org.apache.pdfbox.util.PDFStreamEngine.processOperator(PDFStreamEngine.java:562)
    at org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:269)
    at org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:236)
    at org.apache.pdfbox.util.PDFStreamEngine.processStream(PDFStreamEngine.java:216)
    at org.apache.pdfbox.pdfviewer.PageDrawer.drawPage(PageDrawer.java:139)
    at org.apache.pdfbox.pdmodel.PDPage.print(PDPage.java:890)
    at java.desktop/sun.lwawt.macosx.CPrinterJob$6.run(CPrinterJob.java:757)
    at java.desktop/sun.lwawt.macosx.CPrinterJob.printAndGetPageFormatArea(CPrinterJob.java:767)
    at java.desktop/sun.lwawt.macosx.CPrinterJob.printLoop(Native Method)
    at java.desktop/sun.lwawt.macosx.CPrinterJob.print(CPrinterJob.java:334)
    at java.desktop/sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1443)
    at org.apache.pdfbox.pdmodel.PDDocument.print(PDDocument.java:1545)
    at org.apache.pdfbox.pdmodel.PDDocument.silentPrint(PDDocument.java:1531)
    at com.xlshopgroup.printingpc.WatchFolder.printPDFFromURL(WatchFolder.java:83)
    at com.xlshopgroup.printingpc.WatchFolder.main(WatchFolder.java:118)
Exception: Error: End-of-File, expected line


我的代码:

import java.nio.file.*;
import java.io.*;
import java.net.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;

import java.awt.print.PrinterJob;
import java.awt.print.PageFormat;
import java.awt.print.Book;
import java.awt.print.*;
import java.awt.print.PrinterJob;
import javax.print.PrintService;

import javax.net.ssl.HttpsURLConnection;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.PrintPDF;
import org.apache.commons.*;
import org.apache.fontbox.*;

import com.google.gson.*;

public class WatchFolder {

public static JsonObject sendPost(
        String APIURL
) throws Exception {

    String APIBASEURL = "http://example.com";
    URL obj = new URL(APIBASEURL+APIURL);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    //HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    con.setRequestMethod("POST");

    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.flush();

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    JsonObject convertedObject = new Gson().fromJson(response.toString(), JsonObject.class);
    return convertedObject;

}

public static void printPDFFromURL(
        String pdfURL,
        String printerName
) throws IOException,PrinterException {

    PDDocument document         = null;

    try {
        document = PDDocument.load(new URL(pdfURL));
        PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setJobName(pdfURL);

        if(printerName != null ) {
            PrintService[] printService = PrinterJob.lookupPrintServices();
                boolean printerFound = false;
                for(int i = 0;!printerFound && i < printService.length; i++) {
                    if(printService[i].getName().indexOf(printerName) != -1) {
                        printJob.setPrintService(printService[i]);
                        printerFound = true;
                    System.out.println("Printer found: " + printService[i].getName());
                    }
                }
        }

        document.silentPrint( printJob );

    }catch(Exception e) {

        System.out.println("Exception: "+e.getMessage());

    }finally {

        if(document != null) {

            document.close();

        }

    }

}

public static void main(
        String[] args
) throws Exception {

    JsonObject toBePrinted  = sendPost("gettoprint");
    JsonObject response     = toBePrinted.getAsJsonObject("response");
    JsonArray  results      = response.getAsJsonArray("results");

    for (JsonElement result : results) {

        JsonObject resultObj        = result.getAsJsonObject();

        String PackingSlipURL       = resultObj.get("packing_slip_url").getAsString();
        String StickerURL           = resultObj.get("sticker_url").getAsString();
        String ShippingPartner      = resultObj.get("shipping_partner").getAsString();
        String id                   = resultObj.get("id").getAsString();

        printPDFFromURL(PackingSlipURL, "HP-idealbetalingen-printer");
        printPDFFromURL(StickerURL, "Zebra?");

        //JsonObject deletedFromQueue   = sendPost("deletefromqueue/"+id);

    }

}

}

最佳答案

@自;确实正如约翰·凯恩(John Kane)所建议的那样,它与版本编号有关。我正在尝试使用2.x.x版本的fontbox和1.8.x版本的pdfbox。

09-03 20:59