月度归档:2024年10月

使用浏览器下载合并PDF

<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) {
            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);

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

    </script>

调用:

mergeAllPDFs([“/file/save/2024-10-31-fdad0790-bef7-4d0c-bc0f-988c056fe489.pdf”,”/file/save/2024-10-31-22c2e761-a2e2-4459-b5a6-a67f78c1ce25.pdf”])

mergeAllPDFs([“bc089.pdf”,”5.pdf”])两个文件合并下载

python Mysql

import time
import pymysql
import json

base_dir="/home/wwwroot/test.acfun.org.cn/public/upload/"
with open('config.json') as f:
    info=json.loads(f.read())


db = pymysql.connect(host="localhost",user=info['dbUser'],password=info['dbPassword'],database=info['dbName'],port=3306 )
cursor = db.cursor()
cursor.execute("SELECT tid,or_file FROM cmf_pdf WHERE done='0' AND del='0' ")
data = cursor.fetchone()

ESP32P4 circuitpython

circuitpython 9.2.0 CPU 360MHZ RAM 33038080

圆周率计算如下图,对比rp2350(使用Micropython 1.24固件)在300MHZ下的计算圆周率速度相仿,但是未使用Micropython下比较。

处理器CPURAM版本10050010002000500010000
RP23501504868481.2411822791071878242860
RP23502504868481.24749167643527025717
RP23503004868481.24541139536439121430
RP2350(不稳定)3204868481.24538131503411620091
ESP32P4360330380809.2.04.3945341.9922163.086532.2273068.8511600.1

pi_circuitpython.py · micropython中文社区/PiCalcTest – Gitee.com

整合到一起

代码整合

from PIL import Image
import os

class do_it(object):
    def __init__(self):
        self.download="./pdf/"
        # 保存目录
    def create_img_pdf(self,file_name,ouput_pdf):
        page = Image.new('RGB', (2480, 3508), 'white')
        image=Image.open(file_name)
        i_w,i_h=image.size
        if i_w>i_h:
            i_n_w=2000
            i_n_h=int(i_h*2000/i_w)
        else:
            i_n_h=3000
            i_n_w=int(i_w*3000/i_h)
        n_img=image.resize((i_n_w,i_n_h))
        page.paste(n_img,(100,100))
        page.save(ouput_pdf,"PDF", resolution=100.0)
    def topdf(self,nfile):
        #保存在当前的pdf文件夹
        # nfile必须独一无二
        com="libreoffice --convert-to pdf:writer_pdf_Export "+nfile+" --outdir "+self.download
        os.system(com)
        name=nfile[nfile.rfind('/')+1:nfile.rfind('.')]+".pdf"
        newa=self.download+name
        return newa
    def combine(self,file_list,out_file):
        file_info=""
        for i in file_list:
            file_info=file_info+" "+i
        com="pdftk "+file_info+" cat output "+out_file
        os.system(com)