如何解决java.lang.NoSuchMethodError:org.apache.fontbox.afm.AFMParser.ICEF()V?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (2737)

Error log:

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

code:

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);

    }

}

}
提问于
用户回答回答于

这与版本号有关,使用一个2.x.x版本的fontbox和1.8.x版本的pdfbox

用户回答回答于

当我使用1.8.xPDFBox和2.0.0Fonbox时,得到了完全相同的异常

把我的方框改成了1.5.0,起了作用

扫码关注云+社区

领取腾讯云代金券