优秀的编程知识分享平台

网站首页 > 技术文章 正文

网页转pdf,这个工具真好用(网页转pdf,这个工具真好用啊)

nanyue 2024-08-01 22:51:28 技术文章 7 ℃

前两天有个客户需要把网页转为pdf,之前也没开发过类似的工具,就在百度搜索了一波,主要有下面三种

  1. 在线转pdf
  2. 使用浏览器打印功能转pdf
  3. 使用本地软件工具转pdf

在线转pdf

在百度(我一般用必应)搜索“在线网页转pdf”就有很多可以做这个事的网站,免费的如

  • PDF24Tools

各种pdf的操作都有,免费使用,速度一般。

官网地址https://tools.pdf24.org/zh

  • doctron

开源免费项目,使用golang写的,提供在线转

官网地址http://doctron.lampnick.com/

还有挺多其他的,可以自己搜索,但是都不符合我的预期。

使用浏览器打印功能转pdf

  1. 在浏览器右键,点击打印或者ctrl+p
  2. 在弹出的打印对话框中找到目标打印机选择“另存为PDF”
  3. 点击“保存”按钮即可下载pdf了

使用本地软件工具转pdf

Doctron,这是我今天要介绍的重头戏。

Doctron是基于Docker、无状态、简单、快速、高质量的文档转换服务。目前支持将html转为pdf、图片(使用chrome(Chromium)浏览器内核,保证转换质量)。支持PDF添加水印。

  • 使用chrome内核保证高质量将HTML转为pdf/图片。
  • 简易部署(提供docker镜像,Dockerfile以及k8s yaml配置文件)。支持丰富的转换参数。转为pdf和图片支持自定义大小。
  • 无状态服务支持。

管他的,先把代码下载下来再说

git clone https://gitcode.net/mirrors/lampnick/doctron.git

运行

go build
./doctron --config conf/default.yaml

转pdf,访问http://127.0.0.1:8080/convert/html2pdf?u=doctron&p=lampnick&url=<url>,更换链接中的url为你需要转换的url即可。

然后就可以写程序去批量转换需要的网页了,但是我需要转换的网页有两个需求

1、网站需要会员登录,不然只能看得到一部分

2、需要把网站的头和尾去掉的

这就为难我了,不会go语言啊,硬着头皮搞了,肯定有个地方打开这个url的,就去代码慢慢找,慢慢调试,功夫不负有心人,终于找到调用的地方了。

第一步:添加网站用户登录cookie

第二步:去掉网站头尾

chromedp.Evaluate(`$('.header').css("display" , "none");
		$('.btn-group').css("display" , "none");
		$('.container .container:first').css("display" , "none");
		$('.breadcrumb').css("display" , "none");
		$('.footer').css("display" , "none")`, &ins.buf),

打开网页后执行js代码把头尾隐藏掉

第三步:程序化,批量自动生成pdf

public static void createPDF(String folder , String cl ,  String pdfFile, String urlhref) {
        try {
            String fileName = pdfFile.replace("/", ":");
            String filePath = folder + fileName;
            File srcFile = new File(filePath);
            File newFolder = new File("/Volumes/disk2/myproject" + File.separator + cl);
            File destFile = new File(newFolder, fileName);
            if(destFile.exists()){
                return;
            }
            if(srcFile.exists()){
                //移动到对应目录
                if(!newFolder.exists()){
                    newFolder.mkdirs();
                }
                FileUtils.moveFile(srcFile , destFile);
                return;
            }
            if(!newFolder.exists()){
                newFolder.mkdirs();
            }
            String url = "http://127.0.0.1:8888/convert/html2pdf?u=doctron&p=lampnick&url="+urlhref;
            HttpEntity<String> entity = new HttpEntity<String>(null, null);
            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity<byte[]> bytes = restTemplate.exchange(url, HttpMethod.GET, entity, byte[].class);
            if (bytes.getBody().length <= 100) {
                if(urlList.containsKey(urlhref)){
                    Integer failCount = urlList.get(urlhref);
                    if(failCount > 3){
                        System.out.println("下载失败:" + cl + " / " + pdfFile +"  " + urlhref);
                        return;
                    }
                    failCount++;
                    urlList.put(urlhref , failCount);
                }else{
                    urlList.put(urlhref , 1);
                }

                createPDF(folder , cl ,  pdfFile , urlhref);
            }else{
                if (!destFile.exists()) {
                    try {
                        destFile.createNewFile();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                try (FileOutputStream out = new FileOutputStream(destFile);) {
                    out.write(bytes.getBody(), 0, bytes.getBody().length);
                    out.flush();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

最终成果:


Tags:

最近发表
标签列表