首页 > 解决方案 > PDFTron HTML 到 PDF 转换失败和错误代码 0

问题描述

下面是我们用来将 html 转换为 pdf 的代码。它在 Windows 环境下运行良好,但相同的代码在 linux 环境下无法运行。我还检查了 tempPath 目录是否具有读/写访问权限。“转换失败。HTTP 代码:0”converter.getLog() 为空

为下面的 pdftron 设置临时路径是行

PDFNet.setTempPath(tempPath);
utils.PdfViewerLicense.initialize(); 

将 Html 转换为 pdf 的代码

public static byte[]  generatePDF(String pdfHtml) throws PDFNetException {
  PDFDoc doc = null;
  HTML2PDF converter = null;
  byte [] bytes = null;
  try {
      converter = new HTML2PDF();
      doc = new PDFDoc();
      converter.setLandscape(false);
      converter.insertFromHtmlString(pdfHtml);
      if (converter.convert(doc)) {
          bytes = doc.save( SDFDoc.e_linearized, null);
      } else {
          throw new PDFNetException("", 1L, "", "", "Conversion failed. HTTP Code: " + converter.getHTTPErrorCode() + "\n" + converter.getLog());
      }
  } catch (PDFNetException e) {
      throw e;
  }catch(Exception ex){
      throw new PDFNetException("", 1L, "", "", ex.getMessage());
  }finally {
      if(converter!=null)
          converter.destroy();
      if(doc!=null)
          doc.close();
  }
  return bytes;
}

标签: javapdftron

解决方案


确保 HTML2PDF.so 文件至少对您正在运行的用户代理是可执行的(也许对每个人都是可执行的)。这应该在 html2pdf 下载附带的自述文件中进行描述。


推荐阅读