PDF尺寸修改:等比绽放(标准面单100*150mm)

PDF修改尺寸

需要注意:第一个方法返回的是转换后PDF的base64。第二个方法返回的是文件流,这个方法才是转的核心。

 /**
  * 修改PDF尺寸
  *
  * @param pdfUrl                 PDF链接
  * @param pdfWidthInMillimeters  指定宽 mm
  * @param pdfHeightInMillimeters 指定高 mm
  * @return PDF转换尺寸后的base64
  */
 public static String updatePdfSize(String pdfUrl, float pdfWidthInMillimeters, float pdfHeightInMillimeters) {
     try {
         URL url = new URL(pdfUrl);
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
         conn.setRequestMethod("GET");
         conn.setConnectTimeout(5000);
         InputStream is = conn.getInputStream();
         ByteArrayOutputStream out = updatePdfSize(is, pdfWidthInMillimeters, pdfHeightInMillimeters);
         BASE64Encoder encoder = new BASE64Encoder();
         String pdfBase64 = encoder.encode(out.toByteArray());
         is.close();
         return pdfBase64;
     } catch (IOException e) {
         log.error("updatePdfSize error:{}", e.getMessage(), e);
     }
     return "";
 }
 /**
   * 转换PDF尺寸
   *
   * @param inputStream            PDF源文件流
   * @param pdfWidthInMillimeters  指定宽 mm
   * @param pdfHeightInMillimeters 指定高 mm
   * @return 转换后的PDF的文件流
   */
  public static ByteArrayOutputStream updatePdfSize(InputStream inputStream, float pdfWidthInMillimeters, float pdfHeightInMillimeters) {
      try {
          // 毫米转换为磅(1毫米≈2.83465磅)
          float pdfWidthInPoints = pdfWidthInMillimeters * 2.83465f;
          float pdfHeightInPoints = pdfHeightInMillimeters * 2.83465f;

          //  容器初始化
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          Document doc = new Document();
          PdfWriter writer = PdfWriter.getInstance(doc, out);
          doc.open();
          PdfReader pdfReader = new PdfReader(inputStream);
          PdfContentByte cb = writer.getDirectContent();

          // 循环修改尺寸
          int total = pdfReader.getNumberOfPages();
          for (int i = 1; i <= total; i++) {
              PdfImportedPage page = writer.getImportedPage(pdfReader, i);
              Rectangle rectangle = pdfReader.getPageSize(i);

              float originalWidth = rectangle.getWidth();
              float originalHeight = rectangle.getHeight();

              // 计算缩放比例
              float scaleWidth = pdfWidthInPoints / originalWidth;
              float scaleHeight = pdfHeightInPoints / originalHeight;
              float scale = Math.min(scaleWidth, scaleHeight);

              doc.setPageSize(new RectangleReadOnly(originalWidth * scale, originalHeight * scale));
              doc.newPage();
              cb.addTemplate(page, scale, 0, 0, scale, 0, 0);
          }

          doc.close();
          writer.close();
          return out;
      } catch (Exception e) {
          log.error("updatePdfSize error:{}", e.getMessage(), e);
          return null;
      }
  }

图片转成指大小PDF

返回的是base64。需要返回流的,可能简单改写下

/**
  * 图片转成指定大小的PDF的base64
  *
  * @param pngImagePath           图片地址
  * @param pdfWidthInMillimeters  指定的PDF宽(mm)
  * @param pdfHeightInMillimeters 指定的PDF高(mm)
  * @return PDF的Base64
  */
 public static String convertPngToPdfBase64(String pngImagePath, float pdfWidthInMillimeters, float pdfHeightInMillimeters) {
     try {
         // 毫米转换为磅(1毫米≈2.83465磅)
         float pdfWidthInPoints = pdfWidthInMillimeters * 2.83465f;
         float pdfHeightInPoints = pdfHeightInMillimeters * 2.83465f;

         // 读取PNG图像的原始宽度和高度(单位:点)
         Image pngImage = Image.getInstance(pngImagePath);
         float originalWidth = pngImage.getWidth();
         float originalHeight = pngImage.getHeight();

         // 计算图像在PDF中的缩放比例
         float scaleWidth = pdfWidthInPoints / originalWidth;
         float scaleHeight = pdfHeightInPoints / originalHeight;
         float scale = Math.min(scaleWidth, scaleHeight);

         // 计算图像在PDF中的位置居中显示
         float xPosition = (pdfWidthInPoints - originalWidth * scale) / 2;
         float yPosition = (pdfHeightInPoints - originalHeight * scale) / 2;

         // 创建Document对象,并设置PDF文档的大小为所需尺寸
         Document document = new Document(new Rectangle(pdfWidthInPoints, pdfHeightInPoints));

         // 创建PdfWriter对象,将输出流与Document对象关联
         ByteArrayOutputStream os = new ByteArrayOutputStream();
         PdfWriter.getInstance(document, os);

         // 打开Document
         document.open();

         // 设置PNG图像在PDF中的缩放比例和位置
         pngImage.scaleAbsolute(originalWidth * scale, originalHeight * scale);
         pngImage.setAbsolutePosition(xPosition, yPosition);

         // 将PNG图像添加到PDF中
         document.add(pngImage);

         // 关闭Document
         document.close();

         // 返回PDF的base64
         return new BASE64Encoder().encode(os.toByteArray()).trim().replaceAll("\\r", "").replaceAll("\\n", "");
     } catch (DocumentException | IOException e) {
         log.error("image 转 pdf 流失败 {}", e.getMessage(), e);
         return "";
     }
 }

你可能感兴趣的:(Java代码,pdf,java)