html如何转pdf


当前第2页 返回上一页

3、后端代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

@RequestMapping(value = "/pdf",method = RequestMethod.POST)

    public void test(MultipartHttpServletRequest request, HttpServletResponse response) throws IOException {

        String filePath = "D:\\blog\\exportPdf2.pdf";

        String imagePath = "D:\\blog\\exportImg2.png";

        Document document = new Document();

        try{

            Map getMap = request.getFileMap();

            MultipartFile mfile = (MultipartFile) getMap.get("imgData"); //获取数据

            InputStream file = mfile.getInputStream();

            byte[] fileByte = FileCopyUtils.copyToByteArray(file);

 

            FileImageOutputStream imageOutput = new FileImageOutputStream(new File(imagePath));//打开输入流

            imageOutput.write(fileByte, 0, fileByte.length);//生成本地图片文件

            imageOutput.close();

 

            PdfWriter.getInstance(document, new FileOutputStream(filePath)); //itextpdf文件

            document.open();

            document.add(new Paragraph("JUST TEST ..."));

            Image image = Image.getInstance(imagePath); //itext-pdf-image

            float heigth = image.getHeight();

            float width = image.getWidth();

            int percent = getPercent2(heigth, width);  //按比例缩小图片

            image.setAlignment(Image.MIDDLE);

            image.scalePercent(percent+3);

            document.add(image);

            document.close();

 

        }catch (DocumentException de) {

            System.err.println(de.getMessage());

        }

        catch (Exception e) {

            e.printStackTrace();

 

        }

    }

 

    private static int getPercent2(float h, float w) {

        int p = 0;

        float p2 = 0.0f;

        p2 = 530 / w * 100;

        p = Math.round(p2);

        return p;

    }

4、包名

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

import com.itextpdf.text.Document;

import com.itextpdf.text.DocumentException;

import com.itextpdf.text.Image;

import com.itextpdf.text.Paragraph;

import com.itextpdf.text.pdf.PdfWriter;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.stereotype.Controller;

import org.springframework.util.FileCopyUtils;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.multipart.MultipartFile;

import org.springframework.web.multipart.MultipartHttpServletRequest;

 

import javax.imageio.stream.FileImageOutputStream;

import javax.servlet.http.HttpServletResponse;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.Map;

5、项目源码地址

1

https://github.com/zhangjy520/learn_java/tree/master/boot

推荐学习:html视频教程

以上就是html如何转pdf的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

Html中scrolling是什么意思

Html是一种页面什么型语言?

Html怎么实现下拉菜单

使用Html应该如何编写邮件模版

Html怎么去掉元素的边框

Html怎么设置行距

几种关于Html和css的使用方法

Html怎么设置表格间距

优化Html的输入框提高用户体验和易用度的方法

Html出现网页乱码怎么办

更多相关阅读请进入《Html》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...