【笔记】当个自由的书籍收集者从canvas得到png转pdf

最近有点迷各种古书,然后从 www.shuge.org 下载了各种高清的印本,快成db狂魔了...上面也有人在各种平台上分享,不胜感激...只是有些平台可以免费看但是没法下载... 反正你都canvas了,撸下来自己珍藏...

于是让qwen写了一段代码:

write a javascript function to trigger download a picture from a canvas image data. i mean there is a canvas with sth painted. i'd like to have a function to get its image data and convert the raw data into for example png file data array and trigger downloading the file from memory.

js 复制代码
function downloadCanvasImage(canvas, filename = 'canvas-image.png', format = 'png') {
    // Get the image data URL from the canvas
    const mimeType = format === 'png' ? 'image/png' : 'image/jpeg';
    const dataURL = canvas.toDataURL(mimeType);

    // Create a temporary link element
    const link = document.createElement('a');
    link.href = dataURL;
    link.download = filename;

    // Append the link to the body (required for Firefox)
    document.body.appendChild(link);

    // Programmatically click the link to trigger the download
    link.click();

    // Remove the link from the DOM
    document.body.removeChild(link);
}

之后

js 复制代码
const list = document.querySelectorAll('canvas.<classname>');
for (let i = 0; i < list.length; i ++) {
   downloadCanvasImage(list[i], `page${i+1}.png`);
}

然后再一个 image-to-pdf 就可以变成pdf存档了。大书库又多了几百GB...

把各种迷信的命相卜书籍看完,大致了解了古代历法五行阴阳历干支历(圭表测影、十月历、六龙历...),易经卦象...开始再看一遍医书,启动第二遍理解...

相关推荐
全栈老石2 天前
手写无限画布4 —— 从视觉图元到元数据对象
前端·javascript·canvas
全栈老石2 天前
手写一个无限画布 #3:如何在Canvas 层上建立事件体系
前端·javascript·canvas
百事牛科技3 天前
保护文档安全:PDF限制功能详解与实操
windows·pdf
开开心心就好3 天前
安卓开源应用,超时提醒紧急人护独居安全
windows·决策树·计算机视觉·pdf·计算机外设·excel·动态规划
全栈老石3 天前
手写一个无限画布 #2:渲染层的博弈:Canvas 还是 WebGL ?
javascript·canvas
全栈老石4 天前
手写一个无限画布 #1:坐标系的谎言
前端·canvas
Felicia-侧听4 天前
如何统一PDF页面宽度?统一pdf宽度的2种方法
pdf·pdf宽度统一
开开心心_Every4 天前
音频格式互转工具,支持Mp3ApeWavFlac互转
linux·运维·服务器·typescript·edge·pdf·asp.net
2501_930707784 天前
如何使用C#代码从 PDF 中提取表格并另存为Excel文件
pdf·excel
悟乙己4 天前
实施手册:如何自己构建财务PDF文件内容抽取引擎
pdf