建设银行网站打印账单今天刚刚发生的新闻最新新闻
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);// 打开Documentdocument.open();// 设置PNG图像在PDF中的缩放比例和位置pngImage.scaleAbsolute(originalWidth * scale, originalHeight * scale);pngImage.setAbsolutePosition(xPosition, yPosition);// 将PNG图像添加到PDF中document.add(pngImage);// 关闭Documentdocument.close();// 返回PDF的base64return new BASE64Encoder().encode(os.toByteArray()).trim().replaceAll("\\r", "").replaceAll("\\n", "");} catch (DocumentException | IOException e) {log.error("image 转 pdf 流失败 {}", e.getMessage(), e);return "";}}