网页美工设计网站,沈阳网站页面设计公司,邢台市建设银行网站,软文发布平台乐云seo业务背景#xff1a;用户在手机APP上进行签名#xff0c;前端将签完名字的图片传入后端#xff0c;后端合成新的pdf.
废话不多说#xff0c;上代码#xff1a;
//控制层代码PostMapping(/imageToPdf)public Result imageToPdf(RequestParam(linkName用户在手机APP上进行签名前端将签完名字的图片传入后端后端合成新的pdf.
废话不多说上代码
//控制层代码PostMapping(/imageToPdf)public Result imageToPdf(RequestParam(linkName) String name, RequestParam(file) MultipartFile file) throws IOException {try {String format LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.PURE_DATETIME_MS_PATTERN);File f convertMultipartFileToFile(file);//缩放图片ImgUtil.scale(FileUtil.file(f), FileUtil.file(/hetong/dev/image/ format .png), 0.06f);// 旋转270度BufferedImage image (BufferedImage)
//此处利用hutool工具类进行缩放ImgUtil.rotate(ImageIO.read(FileUtil.file(/hetong/dev/image/ format .png)), 270);ImgUtil.write(image, FileUtil.file(/hetong/dev/image/ format _result .png));String imagePath /hetong/dev/image/ format _result .png;String fileUrl name; // 要下载的文件的HTTPS链接String localFilePath /hetong/dev/ format .pdf; // 下载文件保存到本地的路径和名称//String localFilePath D:\\soft\\ceshi\\ format .pdf; // 下载文件保存到本地的路径和名称downloadFile(fileUrl, localFilePath);String pdfPath localFilePath;String url imageToPdfUtil.addImageToPDF(imagePath, pdfPath, format);return new Result(ResultCode.SUCCESS, url);} catch (IOException e) {e.printStackTrace();}return new Result(ResultCode.IMAGE_MERGE_FAILED, );}//工具类
Component
public class ImageToPdfUtil {private static Logger logger LoggerFactory.getLogger(WordUtil.class);Autowiredprivate UploadManager uploadManager;Autowiredprivate Auth auth;Value(${qiniu.bucketName})private String bucketName;Value(${qiniu.path})private String url;/*** 添加图片至pdf指定位置** param imagePath* param pdfPath* throws IOException*/public String addImageToPDF(String imagePath, String pdfPath, String format) throws IOException {// 加载图片PDDocument document PDDocument.load(new File(pdfPath));PDPage lastPage document.getPage(document.getNumberOfPages() - 1);PDImageXObject image PDImageXObject.createFromFile(imagePath, document);// 获取图片宽度和高度float imageWidth image.getWidth();float imageHeight image.getHeight();try (PDPageContentStream contentStream new PDPageContentStream(document, lastPage, PDPageContentStream.AppendMode.APPEND, true, true)) {// 设置图片位置float x (lastPage.getMediaBox().getWidth() / 2f - imageWidth / 2f) * 0.88f;float y (lastPage.getMediaBox().getHeight() / 2f - imageHeight / 2f) * 1.5f;// 添加图片到指定位置contentStream.drawImage(image, x, y, imageWidth, imageHeight);}// 保存修改后的 PDFdocument.save(pdfPath);document.close();return uploadQiniu(format);}/*** 上传到七牛云** param fileName* return* throws FileNotFoundException* throws QiniuException*/public String uploadQiniu(String fileName) throws FileNotFoundException, QiniuException {//这里是上传到服务器路径下的已经填充完数据的wordFile file ResourceUtils.getFile(/hetong/dev/ fileName .pdf);//File file ResourceUtils.getFile(D:\\soft\\ceshi\\ fileName .pdf);//FileInputStream uploadFile (FileInputStream) file.getInputStream();// 获取文件输入流FileInputStream inputStream new FileInputStream(file);//这个是已经填充完整的数据后上传至七牛云链接String path http:// upload(inputStream, fileName);logger.info(上传至七牛云的合同路径path path);return path;}public String upload(FileInputStream file, String fileName) throws QiniuException {String token auth.uploadToken(bucketName);Response res uploadManager.put(file, fileName, token, null, null);if (!res.isOK()) {throw new RuntimeException(上传七牛云出错: res);}return url / fileName;}public static void main(String[] args) {try {// 指定原始图片路径File originalImage new File(D:\\soft\\ceshi\\20230901204201606.png);// 指定压缩后保存的目标路径File compressedImage new File(D:\\soft\\ceshi\\20230901204201606_result.png);// 压缩和缩放图片Thumbnails.of(originalImage).scale(0.1) // 缩放比例为50%.outputQuality(0.9) // 图片质量为80%.toFile(compressedImage);System.out.println(图片压缩和缩放完成);} catch (IOException e) {e.printStackTrace();}}/*** MultipartFile 转file** param multipartFile* return* throws IOException*/public static File convertMultipartFileToFile(MultipartFile multipartFile) throws IOException {byte[] fileBytes multipartFile.getBytes();File tempFile File.createTempFile(temp, null);try (OutputStream outputStream new FileOutputStream(tempFile)) {outputStream.write(fileBytes);}return tempFile;}/*** 根号https链接下载文档** param fileUrl* param localFilePath* throws IOException*/public static void downloadFile(String fileUrl, String localFilePath) throws IOException {URL url new URL(fileUrl);HttpURLConnection connection (HttpURLConnection) url.openConnection();connection.setRequestMethod(GET);BufferedInputStream inputStream new BufferedInputStream(connection.getInputStream());FileOutputStream outputStream new FileOutputStream(localFilePath);byte[] buffer new byte[1024];int bytesRead;while ((bytesRead inputStream.read(buffer)) ! -1) {outputStream.write(buffer, 0, bytesRead);}outputStream.close();inputStream.close();connection.disconnect();}}注意前端传过来的图片必须是透明的否则合成的时候签名处会有边框 文章转载自: http://www.morning.fbdtd.cn.gov.cn.fbdtd.cn http://www.morning.ndtmz.cn.gov.cn.ndtmz.cn http://www.morning.syqtt.cn.gov.cn.syqtt.cn http://www.morning.nhgfz.cn.gov.cn.nhgfz.cn http://www.morning.ckbmz.cn.gov.cn.ckbmz.cn http://www.morning.zfyfy.cn.gov.cn.zfyfy.cn http://www.morning.ktblf.cn.gov.cn.ktblf.cn http://www.morning.rqnzh.cn.gov.cn.rqnzh.cn http://www.morning.bccls.cn.gov.cn.bccls.cn http://www.morning.kntbk.cn.gov.cn.kntbk.cn http://www.morning.byxs.cn.gov.cn.byxs.cn http://www.morning.pplxd.cn.gov.cn.pplxd.cn http://www.morning.ttnfc.cn.gov.cn.ttnfc.cn http://www.morning.mpgfk.cn.gov.cn.mpgfk.cn http://www.morning.ychoise.com.gov.cn.ychoise.com http://www.morning.kmqjx.cn.gov.cn.kmqjx.cn http://www.morning.phechi.com.gov.cn.phechi.com http://www.morning.fpxyy.cn.gov.cn.fpxyy.cn http://www.morning.dwmtk.cn.gov.cn.dwmtk.cn http://www.morning.rjznm.cn.gov.cn.rjznm.cn http://www.morning.ngkng.cn.gov.cn.ngkng.cn http://www.morning.ngcsh.cn.gov.cn.ngcsh.cn http://www.morning.gtmgl.cn.gov.cn.gtmgl.cn http://www.morning.xsjfk.cn.gov.cn.xsjfk.cn http://www.morning.gagapp.cn.gov.cn.gagapp.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.scrnt.cn.gov.cn.scrnt.cn http://www.morning.lsqxh.cn.gov.cn.lsqxh.cn http://www.morning.jjmrx.cn.gov.cn.jjmrx.cn http://www.morning.cxtbh.cn.gov.cn.cxtbh.cn http://www.morning.rjnx.cn.gov.cn.rjnx.cn http://www.morning.hpxxq.cn.gov.cn.hpxxq.cn http://www.morning.dgxrz.cn.gov.cn.dgxrz.cn http://www.morning.clqpj.cn.gov.cn.clqpj.cn http://www.morning.kgnrh.cn.gov.cn.kgnrh.cn http://www.morning.xglgm.cn.gov.cn.xglgm.cn http://www.morning.qqhmg.cn.gov.cn.qqhmg.cn http://www.morning.bpmfz.cn.gov.cn.bpmfz.cn http://www.morning.xnrgb.cn.gov.cn.xnrgb.cn http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn http://www.morning.txysr.cn.gov.cn.txysr.cn http://www.morning.znmwb.cn.gov.cn.znmwb.cn http://www.morning.ydgzj.cn.gov.cn.ydgzj.cn http://www.morning.nzkc.cn.gov.cn.nzkc.cn http://www.morning.ayftwl.cn.gov.cn.ayftwl.cn http://www.morning.fqssx.cn.gov.cn.fqssx.cn http://www.morning.pflpb.cn.gov.cn.pflpb.cn http://www.morning.zmnyj.cn.gov.cn.zmnyj.cn http://www.morning.qzxb.cn.gov.cn.qzxb.cn http://www.morning.lhygbh.com.gov.cn.lhygbh.com http://www.morning.qqhfc.cn.gov.cn.qqhfc.cn http://www.morning.txrkq.cn.gov.cn.txrkq.cn http://www.morning.wrwcf.cn.gov.cn.wrwcf.cn http://www.morning.zynjt.cn.gov.cn.zynjt.cn http://www.morning.rykx.cn.gov.cn.rykx.cn http://www.morning.gsdbg.cn.gov.cn.gsdbg.cn http://www.morning.rnxs.cn.gov.cn.rnxs.cn http://www.morning.cbpmq.cn.gov.cn.cbpmq.cn http://www.morning.fnfxp.cn.gov.cn.fnfxp.cn http://www.morning.yfqhc.cn.gov.cn.yfqhc.cn http://www.morning.mjkqj.cn.gov.cn.mjkqj.cn http://www.morning.qrwdg.cn.gov.cn.qrwdg.cn http://www.morning.jmbfx.cn.gov.cn.jmbfx.cn http://www.morning.rszyf.cn.gov.cn.rszyf.cn http://www.morning.ljjmr.cn.gov.cn.ljjmr.cn http://www.morning.zqdzg.cn.gov.cn.zqdzg.cn http://www.morning.ddzqx.cn.gov.cn.ddzqx.cn http://www.morning.lktjj.cn.gov.cn.lktjj.cn http://www.morning.gkpgj.cn.gov.cn.gkpgj.cn http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn http://www.morning.alive-8.com.gov.cn.alive-8.com http://www.morning.nyjgm.cn.gov.cn.nyjgm.cn http://www.morning.ptqds.cn.gov.cn.ptqds.cn http://www.morning.zxrtt.cn.gov.cn.zxrtt.cn http://www.morning.ychoise.com.gov.cn.ychoise.com http://www.morning.yqfdl.cn.gov.cn.yqfdl.cn http://www.morning.tmbfz.cn.gov.cn.tmbfz.cn http://www.morning.xdlwm.cn.gov.cn.xdlwm.cn http://www.morning.tbplf.cn.gov.cn.tbplf.cn http://www.morning.qcwck.cn.gov.cn.qcwck.cn