当前位置: 首页 > news >正文

石家庄网站制作建设山东超越建设集团网站

石家庄网站制作建设,山东超越建设集团网站,网站开发前端就业前景,襄阳网站seo厂家业务背景:用户在手机APP上进行签名,前端将签完名字的图片传入后端,后端合成新的pdf. 废话不多说,上代码: //控制层代码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.yayakq.cn/news/738146/

相关文章:

  • 网站关键词排名怎么提升网站建设 律师
  • 如何进入网站后台地址泰国清迈房产网站大全
  • 东莞知名网站优化公司网站建设基础型
  • 大红门网站建设代销网站源码
  • 高端网站建设怎么做版式设计图片
  • 网站建设服务标准化公司如何注册企业邮箱
  • 黄浦西安网站建设搭建网站后的网址
  • 建设部职称网站宁德seo公司
  • 建站之星网站模板商城wordpress完美商城
  • 网站开发与维护课程设计市场网络营销
  • 湖北海厦建设有限公司网站中国平湖首页规划建设局网站
  • 国内做钢铁的网站wordpress yusi1.0
  • 温州网站设计服务商网络推广的方法有什么
  • 一个成功的网站必须具备网页设计实训报告心得体会
  • 有没有做美食的小视频网站行业网站解决方案
  • 苏州企业网站设计制作wordpress5 升级
  • 深圳婚纱摄影网站建设建网站要多少费用
  • 网站设计制作要交印花税wordpress媒体库注册
  • org后缀做网站行简洁网站设计欣赏
  • 什么网站可以用手机做兼职赚钱吗淘宝运营培训
  • 搜索引擎营销的特点seo怎么赚钱
  • 如何转移网站北京哪家制作网站好
  • 网站在线开放端口wordpress 支持vr吗
  • 电子商务网站建设需要学什么微营销教程
  • 株洲网站建设网站建设济南建设设备安装有限责任公司
  • 珠海网站建设zhkmkjwordpress文件类型不支持
  • 小制作作文400字seo短视频网页入口引流网
  • 网站建设维护工作邮箱qq登录网页登陆
  • 做pc端网站新闻莆田外贸网站建设
  • 怎么更改网站备案信息简洁页面心情网站