分类目录归档:None

使用PILLOW 将文字列表转为PDF

from PIL import Image, ImageDraw, ImageFont

def img_txt(head_text,file_list):
    width, height = 2480, 3508
    image = Image.new('RGB', (width, height), color = (255, 255, 255))
    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype("/usr/share/fonts/simsun.ttc", 120)
    font2 = ImageFont.truetype("/usr/share/fonts/simsun.ttc", 80)
    head_text="关于文件《"+head_text+"》的附件"
    head_num=len(head_text)
    head_line=head_num//20+1 if head_num%20>0 else head_num//20
    for i in range(head_line):
        text_position = (20, 20+i*120)  # 文字的位置
        draw.text(text_position, head_text[i*20:i*20+20], font=font, fill=(0, 0, 0))
    start= 180+i*120
    j=0
    for file_one in file_list:
        one=str(j+1)+"."+file_one
        one_num=len(one)
        one_line=one_num//30+1 if one_num%30>0 else one_num//30
        x=0
        for x in range(one_line):
            if x>0:
                j=j+1
                text_position = (120, start+j*100)
            else:
                text_position = (80, start+j*100)  # 文字的位置
            draw.text(text_position, one[x*30:x*30+30], font=font2, fill=(0, 0, 0))
            x=x+1
        j=j+1
    image.save('hello.pdf',"PDF",resolution=300)

1.A4 分辨率为300DPI下2480×3508,需要再保存的时候设置DPI,resolution=300

文字列表转图片

from PIL import Image, ImageDraw, ImageFont

# 创建一个新的白色图片
width, height = 2481, 3507
image = Image.new('RGB', (width, height), color = (255, 255, 255))

# 准备画笔
draw = ImageDraw.Draw(image)

# 选择字体和大小
font = ImageFont.truetype("/usr/share/fonts/simsun.ttc", 120)
font2 = ImageFont.truetype("/usr/share/fonts/simsun.ttc", 100)
# 写入文字 20长度
text = "120关于议题《所发生的方式的发生的发送到发送到托付给人生巅峰书房宜》附件"
text_position = (20, 20)  # 文字的位置
draw.text(text_position, text, font=font, fill=(0, 0, 0))

text = "1.这里是第哒哒哒哒哒哒多多多多多顶顶顶顶二行非法"
text_position = (100, 220)  # 文字的位置 23长度
draw.text(text_position, text, font=font2, fill=(0, 0, 0))

text = "2.这里是第三行"
text_position = (100, 420)  # 文字的位置
draw.text(text_position, text, font=font2, fill=(0, 0, 0))

text = "3.这里是第四行"
text_position = (100, 620)  # 文字的位置
draw.text(text_position, text, font=font2, fill=(0, 0, 0))

# 保存图片
image.save('hello.jpg')

print("图片已保存")

120 大小 20个字

100 大小 23个字

JS PDF文件下载,并合并,加动画

CSS代码:

<style>
    /* 加载动画的样式 */
    .loader {
        border: 5px solid #f3f3f3;
        border-radius: 50%;
        border-top: 5px solid #3498db;
        width: 50px;
        height: 50px;
        animation: spin 2s linear infinite;
    }

    @keyframes spin {
        0% {
            transform: rotate(0deg);
        }

        100% {
            transform: rotate(360deg);
        }
    }

</style>

HTML代码

 <button class="btn btn-danger download-files"><span class="glyphicon glyphicon-file"></span></button>

<div id="loadingContainer" style="display:none;margin-top: 10px;" class="row alert alert-info" tag="一键下载附件">
            <div class="col-md-4 col-md-offset-2">
                <h2>下载附件中</h2>
            </div>
            <div class="loader col-md-2"></div>
        </div>



JS代码


        <script src='https://unpkg.com/pdf-lib/dist/pdf-lib.js'></script>
        <script src='https://unpkg.com/pdf-lib/dist/pdf-lib.min.js'></script>

        <script>
            async function mergeAllPDFs(urls) {
                // 显示加载动画
                document.getElementById('loadingContainer').style.display = 'block';

                const pdfDoc = await PDFLib.PDFDocument.create();
                const numDocs = urls.length;

                for (var i = 0; i < numDocs; i++) {
                    const donorPdfBytes = await fetch(urls[i]).then(res => res.arrayBuffer());
                    const donorPdfDoc = await PDFLib.PDFDocument.load(donorPdfBytes);
                    const docLength = donorPdfDoc.getPageCount();
                    for (var k = 0; k < docLength; k++) {
                        const [donorPage] = await pdfDoc.copyPages(donorPdfDoc, [k]);
                        pdfDoc.addPage(donorPage);
                    }
                }

                const pdfBytes = await pdfDoc.save();
                const blob = new Blob([pdfBytes], { type: 'application/pdf' });
                const url = URL.createObjectURL(blob);

                // 隐藏加载动画
                document.getElementById('loadingContainer').style.display = 'none';

                // 创建一个下载链接
                const downloadLink = document.createElement('a');
                downloadLink.href = url;
                downloadLink.download = $("#hy_date").val() + "附件.pdf"; // 指定下载文件名
                document.body.appendChild(downloadLink); // 将链接添加到页面中
                downloadLink.click(); // 模拟点击以下载文件
                document.body.removeChild(downloadLink); // 下载后移除链接
                URL.revokeObjectURL(url); // 释放创建的URL对象
            }
      


            $(".download-files").click(function () {
                $.ajax({
                    url: "{:url('portal/topics/ajax_topics_download_files',['id'=>$info['id']])}",
                    success: function (data) {
                        if (data.status) {
                            let pdfs = [];
                            for (let index = 0; index < data.info.length; index++) {

                            
                                pdfs.push(data.info[index]['pdf']);


                            }
                            console.log(pdfs)
                            mergeAllPDFs(pdfs);
                        }
                    }
                })
            })
        </script>

生成任意字符的单页PDF

中心思想,生成txt文件,再用liberoffice转为PDF

import os

com1="echo "+info_text+":信息 >swap.txt"
com2="libreoffice --convert-to pdf:writer_pdf_Export swap.txt --outdir "+self.download
os.system(com1)
os.system(com2)
  • HTML貌似不可用,目前无法修改字体大小