pom引入jar
<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.0-RC2</version></dependency>
转pdf方法
/*** 使用pdfbox将jpg转成pdf** @throws IOException IOException*/public byte[] jpgToPdf(MultipartFile file) throws IOException {
// long old = System.currentTimeMillis();
// System.out.println(" -- 图片转PDF:" + simpleDateFormat.format(old) + " 开始处理 -- " + fileAbsolutePath);byte[] pdfBytes = new byte[0]; // PDF BytesInputStream jpgStream = file.getInputStream();PDDocument pdDocument = new PDDocument();BufferedImage image = ImageIO.read(jpgStream);PDPage pdPage = new PDPage(new PDRectangle(image.getWidth(), image.getHeight()));pdDocument.addPage(pdPage);PDImageXObject pdImageXObject = LosslessFactory.createFromImage(pdDocument, image);PDPageContentStream contentStream = new PDPageContentStream(pdDocument, pdPage);try {contentStream.drawImage(pdImageXObject, 0, 0, image.getWidth(), image.getHeight());contentStream.close();ByteArrayOutputStream baos = new ByteArrayOutputStream();PDAcroForm acroForm = pdDocument.getDocumentCatalog().getAcroForm();if (acroForm != null) {PDTextField field = (PDTextField) acroForm.getField("sampleField");field.setValue("Text Entry");}pdDocument.save(baos);pdfBytes = baos.toByteArray();
// String newFilePath = fileAbsolutePath.substring(0, fileAbsolutePath.lastIndexOf(".")) + ".pdf";
// pdDocument.save(newFilePath);} catch (IOException e) {e.printStackTrace();} finally {if (pdDocument != null) {try {pdDocument.close();} catch (IOException e) {e.printStackTrace();}}if (jpgStream != null) {try {jpgStream.close();} catch (IOException e) {e.printStackTrace();}}}long now = System.currentTimeMillis();
// File file = new File(fileAbsolutePath);
// boolean delete = file.delete();
// System.out.println(" -- 图片转PDF处理结束时间:" + " 处理结束 -- 删除原始文件 " + delete);return pdfBytes;}
导出方法
@Overridepublic String plantPhotoDownload(DvsPlantPhoto dvsPlantPhoto, HttpServletResponse response, HttpServletRequest request) throws Exception {DvsPlantPhoto result = this.getOne(new QueryWrapper<>(dvsPlantPhoto));if (result != null) {String uri = result.getKey();BlobServiceClient blobServiceClient = BlobUtils.getBlobServiceClient();BlobContainerClient a = BlobUtils.getContainer(blobServiceClient, containerName);ByteArrayOutputStream byteArrayOutputStream = BlobUtils.downBlobFilecao(a, uri);
// return Base64.getEncoder().encodeToString(aa);ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());response.setCharacterEncoding(request.getCharacterEncoding());response.setContentType("application/pdf");try {response.setHeader("Content-Disposition", "attachment; filename=" + result.getKey());IOUtils.copy(byteArrayInputStream, response.getOutputStream());response.flushBuffer();} catch (Exception e) {log.error(e.getMessage(), e);} finally {if (byteArrayInputStream != null) {try {byteArrayInputStream.close();} catch (IOException e) {log.error(e.getMessage(), e);}}if (byteArrayOutputStream != null) {try {byteArrayOutputStream.close();} catch (IOException e) {log.error(e.getMessage(), e);}}}return new JSONObject().toJSONString();} else {return new JSONObject().toJSONString();}}