com.aspose.cad.internal.fY.c.a(Unknown Source)

k8s 上部署的应用 ,处理CAD文件转换成pdf ,就会报
2023-09-25T15:35:45.254530755+08:00 class com.aspose.cad.internal.Exceptions.Exception: Error during PDF export: Cannot invoke “com.aspose.cad.internal.E.M.a(boolean)” because “this.a” is null —> java.lang.NullPointerException: Cannot invoke “com.aspose.cad.internal.E.M.a(boolean)” because “this.a” is null

2023-09-25T15:35:45.254585671+08:00 — End of inner exception stack trace —

2023-09-25T15:35:45.254590172+08:00 com.aspose.cad.internal.fY.c.a(Unknown Source)

2023-09-25T15:35:45.254592694+08:00 com.aspose.cad.w.a(Unknown Source)

2023-09-25T15:35:45.254595277+08:00 com.aspose.cad.internal.b.e.invoke(Unknown Source)

2023-09-25T15:35:45.254597494+08:00 com.aspose.cad.system.Threading.j.invoke(Unknown Source)

2023-09-25T15:35:45.254615731+08:00 com.aspose.cad.system.Threading.n.run(Unknown Source)

2023-09-25T15:35:45.254618582+08:00 java.base/java.lang.Thread.run(Thread.java:833)

2023-09-25T15:35:45.254620961+08:00 at com.aspose.cad.internal.fY.c.a(Unknown Source)

2023-09-25T15:35:45.254623311+08:00 at com.aspose.cad.w.a(Unknown Source)

2023-09-25T15:35:45.254625652+08:00 at com.aspose.cad.internal.b.e.invoke(Unknown Source)

2023-09-25T15:35:45.254627920+08:00 at com.aspose.cad.system.Threading.j.invoke(Unknown Source)

2023-09-25T15:35:45.254630201+08:00 at com.aspose.cad.system.Threading.n.run(Unknown Source)

2023-09-25T15:35:45.254635592+08:00 at java.base/java.lang.Thread.run(Thread.java:833)

2023-09-25T15:35:45.254655622+08:00 Caused by: java.lang.NullPointerException: Cannot invoke “com.aspose.cad.internal.E.M.a(boolean)” because “this.a” is null

2023-09-25T15:35:45.254671759+08:00 at com.aspose.cad.internal.E.K.f(Unknown Source)

2023-09-25T15:35:45.254678899+08:00 at com.aspose.cad.internal.p.M.a(Unknown Source)

2023-09-25T15:35:45.254681617+08:00 at com.aspose.cad.internal.p.M.a(Unknown Source)

2023-09-25T15:35:45.254683956+08:00 at com.aspose.cad.internal.p.M.a(Unknown Source)

2023-09-25T15:35:45.254686153+08:00 at com.aspose.cad.internal.fu.i.a(Unknown Source)

2023-09-25T15:35:45.254688581+08:00 at com.aspose.cad.internal.fu.i.a(Unknown Source)

2023-09-25T15:35:45.254692644+08:00 at com.aspose.cad.internal.fx.i.a(Unknown Source)

2023-09-25T15:35:45.254695070+08:00 at com.aspose.cad.internal.fx.i.a(Unknown Source)

2023-09-25T15:35:45.254697490+08:00 at com.aspose.cad.internal.fx.c.a(Unknown Source)

2023-09-25T15:35:45.254699768+08:00 at com.aspose.cad.internal.fx.c.a(Unknown Source)

2023-09-25T15:35:45.254702095+08:00 at com.aspose.cad.internal.fx.c.a(Unknown Source)

2023-09-25T15:35:45.254706027+08:00 at com.aspose.cad.internal.fn.F.a(Unknown Source)

2023-09-25T15:35:45.254708349+08:00 at com.aspose.cad.internal.fl.k.a(Unknown Source)

2023-09-25T15:35:45.254710573+08:00 at com.aspose.cad.internal.fl.k.b(Unknown Source)

2023-09-25T15:35:45.254712861+08:00 at com.aspose.cad.internal.fY.b.a(Unknown Source)

但是在本地 windows上 就没问题

@huangzx,
你好。
请附上有问题的文件和代码,以便我们可以重现该问题。

public synchronized File doCadToPdf(InputStream inputStream, String outputFilePath)  throws IOException {

    File outputFile = new File(outputFilePath);
    LoadOptions opts = new LoadOptions();
    opts.setSpecifiedEncoding(CodePages.SimpChinese);
    CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
    cadRasterizationOptions.setBackgroundColor(Color.getWhite());
    cadRasterizationOptions.setPageWidth(fileProperties.getNewImageHeight());
    cadRasterizationOptions.setPageHeight(fileProperties.getNewImageWeight() + 105f);
    cadRasterizationOptions.setAutomaticLayoutsScaling(true);
    cadRasterizationOptions.setNoScaling(false);
    cadRasterizationOptions.setDrawType(1);
    PdfOptions pdfOptions = new PdfOptions();
    pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
    try (Image cadImage = Image.load(inputStream, opts);
         OutputStream stream = new FileOutputStream(outputFile)) {
        cadImage.save(stream, pdfOptions);
        return outputFile;
    }
}

CS01-P3-001__1.zip (33.4 KB)
文件

@huangzx
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CADJAVA-11261

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@huangzx,
你能测试一下这是否有效吗?

try {

    String sourcePath = "CS01-P3-001__1.dxf";
    String targetPath = "CS01-P3-001__1.pdf";       

    InputStream inputStream = new FileInputStream(sourcePath);

    LoadOptions opts = new LoadOptions();
    opts.setSpecifiedEncoding(CodePages.SimpChinese);           

    Image cadImage = Image.load(inputStream, opts);         

    CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
    cadRasterizationOptions.setBackgroundColor(Color.getWhite());
    cadRasterizationOptions.setPageWidth(cadImage.getWidth());
    cadRasterizationOptions.setPageHeight(cadImage.getHeight());
    cadRasterizationOptions.setAutomaticLayoutsScaling(true);
    cadRasterizationOptions.setNoScaling(false);
    cadRasterizationOptions.setDrawType(1);
    PdfOptions pdfOptions = new PdfOptions();
    pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);

    OutputStream stream = new FileOutputStream(targetPath);
    cadImage.save(stream, pdfOptions);

} catch (Exception e) {
    e.printStackTrace();
}

刚刚试了,还是一样的错,在本地windows上是可以的,部署到 k8s上就报错

@huangzx,
请给我们更多信息。 你能分享你的dockerfile吗? Aspose.CAD 是什么版本? 您能否分享更多代码,例如您如何调用此函数,fileProperties 是什么? 也许,您可以创建一个完整的示例,我们可以运行并查看错误? 谢谢。

配置文件 就是使用了默认的

/**
 * @author huangzx
 * @title: FileProperties
 * @projectName file-companion
 * @description:
 * @date 8/31/2023 17:23
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "fc.file")
@Slf4j
public class FileProperties {


    private static final String DOWNLOAD_OUT = "drawing-convert-download";
    private static final String PDF_OUT = "pdf-out";
    private static final String CAD_OUT = "cad-out";

    /**
     * 网络文件下载重试次数
     */
    private Integer downloadRetries = 5;

    /**
     * 临时文件夹,存储下载待处理文件、处理结果图片。(默认系统临时文件夹)
     */
    private String downloadPath;

    /**
     * 最小宽度(A4纸:1240像素)
     */
    private int newImageWeight = 1241;

    /**
     * 最小高度度(A4纸:1754像素)
     */
    private int newImageHeight = 1754;

    /**
     * 平台类型
     */
    private DrawingPlatformType platformType;

    /**
     * 云端上传Bucket桶(涉及到访问url时效)
     */
    private String clodUploadBucket;


    /**
     * PDF 文件输出文件路径
     */
    private String pdfOutputPath;

    /**
     * CAD 文件输出路径
     */
    private String cadOutputPath;

    /**
     * 字体
     */
    private Font myFont;

    @PostConstruct
    public void newFileProperties() {

        // 临时文件夹,存储下载待处理文件、处理结果图片。(默认系统临时文件夹)
        if (StringUtils.isNotBlank(this.getDownloadPath())) {
            // 手动补全路径分隔符
            if (this.getDownloadPath().endsWith(File.separator)) {
                 this.setDownloadPath(this.getDownloadPath() + File.separator);
            }
        } else {
            this.setDownloadPath(FolderUtils.getTmpdir() + DOWNLOAD_OUT + File.separator);
        }
        // PDF处理完成后完成后的文件夹
        this.setPdfOutputPath(this.getDownloadPath() + PDF_OUT + File.separator);

        // CAD 文件处理完成后的文件夹
        this.setCadOutputPath(this.getDownloadPath() + CAD_OUT + File.separator);

        File file = new File(this.getDownloadPath());
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (mkdirs) {
                log.info("临时文件夹【" + this.getDownloadPath() + "】创建成功!");
            }
        }

        file = new File(this.getPdfOutputPath());
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (mkdirs) {
                log.info("临时文件输出文件夹【" + this.getPdfOutputPath() + "】创建成功!");
            }
        }

        file = new File(this.getCadOutputPath());
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
            if (mkdirs) {
                log.info("临时CAD文件输出文件夹【" + this.getCadOutputPath() + "】创建成功!");
            }
        }

        try {
            // 自定义图片字体
            InputStream resourceAsStream = FontHelper.class.getResourceAsStream(FontHelper.FONT_PATH);
            if (resourceAsStream == null) {
                log.info("字体未找到:{}", FontHelper.FONT_PATH);
            }
            // 字体大小渲染时确定
            assert resourceAsStream != null;
            Font font = Font.createFont(Font.TRUETYPE_FONT, resourceAsStream).deriveFont(25f);
            this.setMyFont(font);
            log.info("字体初始化成功:{}", font);
        } catch (Exception e) {
            log.error("字体初始化成功异常:", e);
        }

    }
}

处理cad 转换

    public synchronized File doCadToPdf(String url, String pdfFilePath) throws IOException {
        try (InputStream inputStream = HttpURLConnectionHandler.getFileInputStream(url)) {
            return this.doCadToPdf(inputStream, pdfFilePath);
        }
    }

    @Resource
    private FileProperties fileProperties;

    public synchronized File doCadToPdf(String url, String pdfFilePath) throws IOException {
        try (InputStream inputStream = HttpURLConnectionHandler.getFileInputStream(url)) {
            return this.doCadToPdf(inputStream, pdfFilePath);
        }
    }

    public synchronized File doCadToPdf(InputStream inputStream, String outputFilePath)  throws IOException {

        File outputFile = new File(outputFilePath);
        LoadOptions opts = new LoadOptions();
        opts.setSpecifiedEncoding(CodePages.SimpChinese);
        CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
        cadRasterizationOptions.setBackgroundColor(Color.getWhite());
        cadRasterizationOptions.setPageWidth(fileProperties.getNewImageHeight());
        cadRasterizationOptions.setPageHeight(fileProperties.getNewImageWeight() + 105f);
        cadRasterizationOptions.setAutomaticLayoutsScaling(true);
        cadRasterizationOptions.setNoScaling(false);
        cadRasterizationOptions.setDrawType(1);
        PdfOptions pdfOptions = new PdfOptions();
        pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
        try (Image cadImage = Image.load(inputStream, opts);
             OutputStream stream = new FileOutputStream(outputFile)) {
            cadImage.save(stream, pdfOptions);
            return outputFile;
        }
    }

文件下载

    /**
     * 通过url获取文件数据流
     * @param url 文件路径
     * @return InputStream
     * @throws IOException
     */
    public static InputStream getFileInputStream(String url) throws IOException {
        URL realUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
        connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.183");
        connection.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
        connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(10000);
        connection.connect();
        return connection.getInputStream();
    }

最后获得文件的调用

    public File handleCad2Pdf(String downloadUrl) throws IOException {
        String pdfFilePath = fileProperties.getCadOutputPath() + UUID.randomUUID() + ".pdf";
        return cadFileHandler.doCadToPdf(downloadUrl, pdfFilePath);
    }

aspose-cad 版本是 23.4 ; java 用的 java17

1 Like

@huangzx,
请给我们一些时间来调查此案。 谢谢。