.net pdf转图片_在客户端实现PDF转图片
今天在寻找合适的Python下PDF转图片的解决方案时,意外发现pdf.js(
Mozilla贡献的一个基于 web 标准的通用 pdf 解析和渲染库。)也可以实现PDF转图片,并且是在客户端就可以实现,代码也很简洁,
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta name="viewport" content="width=device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=yes"><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js">script><script src="pdf.js">script><script src="pdf.worker.js">script><style type="text/css">#upload-button { width: 150px; display: block; margin: 20px auto;}#file-to-upload { display: none;}#pdf-main-container { width: 400px; margin: 20px auto;}#pdf-loader { display: none; text-align: center; color: #999999; font-size: 13px; line-height: 100px; height: 100px;}#pdf-contents { display: none;}#pdf-meta { overflow: hidden; margin: 0 0 20px 0;}#pdf-buttons { float: left;}#page-count-container { float: right;}#pdf-current-page { display: inline;}#pdf-total-pages { display: inline;}#pdf-canvas { border: 1px solid rgba(0,0,0,0.2); box-sizing: border-box;}#page-loader { height: 100px; line-height: 100px; text-align: center; display: none; color: #999999; font-size: 13px;}#download-image { width: 150px; display: block; margin: 20px auto 0 auto; font-size: 13px; text-align: center;}style>head><body><button id="upload-button">Select PDFbutton> <input type="file" id="file-to-upload" accept="application/pdf" /><div id="pdf-main-container"> <div id="pdf-loader">Loading document ...div> <div id="pdf-contents"> <div id="pdf-meta"> <div id="pdf-buttons"> <button id="pdf-prev">Previousbutton> <button id="pdf-next">Nextbutton> div> <div id="page-count-container">Page <div id="pdf-current-page">div> of <div id="pdf-total-pages">div>div> div> <canvas id="pdf-canvas" width="800">canvas> <div id="page-loader">Loading page ...div> <a id="download-image" href="#">Download PNGa> div>div><script>var __PDF_DOC, __CURRENT_PAGE, __TOTAL_PAGES, __PAGE_RENDERING_IN_PROGRESS = 0, __CANVAS = $('#pdf-canvas').get(0), __CANVAS_CTX = __CANVAS.getContext('2d');function showPDF(pdf_url) { $("#pdf-loader").show(); PDFJS.getDocument({ url: pdf_url }).then(function(pdf_doc) { __PDF_DOC = pdf_doc; __TOTAL_PAGES = __PDF_DOC.numPages; // Hide the pdf loader and show pdf container in HTML $("#pdf-loader").hide(); $("#pdf-contents").show(); $("#pdf-total-pages").text(__TOTAL_PAGES); // Show the first page showPage(1); }).catch(function(error) { // If error re-show the upload button $("#pdf-loader").hide(); $("#upload-button").show(); alert(error.message); });;}function showPage(page_no) { __PAGE_RENDERING_IN_PROGRESS = 1; __CURRENT_PAGE = page_no; // Disable Prev & Next buttons while page is being loaded $("#pdf-next, #pdf-prev").attr('disabled', 'disabled'); // While page is being rendered hide the canvas and show a loading message $("#pdf-canvas").hide(); $("#page-loader").show(); $("#download-image").hide(); // Update current page in HTML $("#pdf-current-page").text(page_no); // Fetch the page __PDF_DOC.getPage(page_no).then(function(page) { // As the canvas is of a fixed width we need to set the scale of the viewport accordingly var scale_required = 2;//__CANVAS.width / page.getViewport(1).width; // Get viewport of the page at required scale var viewport = page.getViewport(scale_required); // Set canvas height __CANVAS.height = viewport.height; var renderContext = { canvasContext: __CANVAS_CTX, viewport: viewport }; // Render the page contents in the canvas page.render(renderContext).then(function() { __PAGE_RENDERING_IN_PROGRESS = 0; // Re-enable Prev & Next buttons $("#pdf-next, #pdf-prev").removeAttr('disabled'); // Show the canvas and hide the page loader $("#pdf-canvas").show(); $("#page-loader").hide(); $("#download-image").show(); }); });}// Upon click this should should trigger click on the #file-to-upload file input element// This is better than showing the not-good-looking file input element$("#upload-button").on('click', function() { $("#file-to-upload").trigger('click');});// When user chooses a PDF file$("#file-to-upload").on('change', function() { // Validate whether PDF if(['application/pdf'].indexOf($("#file-to-upload").get(0).files[0].type) == -1) { alert('Error : Not a PDF'); return; } $("#upload-button").hide(); // Send the object url of the pdf showPDF(URL.createObjectURL($("#file-to-upload").get(0).files[0]));});// Previous page of the PDF$("#pdf-prev").on('click', function() { if(__CURRENT_PAGE != 1) showPage(--__CURRENT_PAGE);});// Next page of the PDF$("#pdf-next").on('click', function() { if(__CURRENT_PAGE != __TOTAL_PAGES) showPage(++__CURRENT_PAGE);});// Download button$("#download-image").on('click', function() { $(this).attr('href', __CANVAS.toDataURL()).attr('download', 'page.png');});script>body>html>
原理就是使用把PDF渲染在画布上,然后把画布里的内容保存为图片。有人说它生成的图片效果不好,其实只要根据需要调整这个变量就可以了。
var scale_required = __CANVAS.width / page.getViewport(1).width;
比如这个例子里的默认取值是画布的宽度除以PDF页面的宽度,如果我们把它改为
var scale_required =2;
效果会理想很多。
.net pdf转图片_在客户端实现PDF转图片相关推荐
- python docx 合并文档 图片_不再为处理PDF烦恼,python处理操作PDF全攻略
本篇聊下Python对pdf的各种操作,包含pdf转word,pdf转图片,pdf翻转,加密,加水印等. pdf转换word文档 保留格式 pdf转换为word文档,被大众经常使用的是纯Python库 ...
- xml文件转换成图片_怎样能把PDF文件转换成图片?
我们的日常生活工作中时常碰到pdf与Excel.Word.ppt和jpg等文件格式的转换,有时候由于工作的需要,要把PDF文件转换成图片.并且现在网上的很多素材都是PDF文件格式的,如果我们想要里面的 ...
- node 压缩图片_推荐10个常用的图片处理小帮手(下)「值得收藏」
作者: semlinker 转发链接:https://mp.weixin.qq.com/s/i3ynTtPJOECoAYfqHFoo3Q 前言 本文给小伙伴们隆重介绍用于图片处理的十个 「" ...
- python给图片添加水印图片_使用Python编写批量添加图片水印程序
文章目录 一.为什么要为图片添加水印 二.利用Python为图片添加水印 遍历文件夹下所有图片文件 三.利用QT for Python制作图片水印添加程序 一.为什么要为图片添加水印 在互联网写文章最 ...
- node 压缩图片_手搓一个TinyPng压缩图片的WebpackPlugin
作者: JowayYoung 转发链接:https://mp.weixin.qq.com/s/eqsZwZPCX-GZyB-EOm3TwQ 前言 曾经发表过一篇性能优化的文章<「实践」细聊前端性 ...
- qt designer 插入图片_真的不错,宁波棉服图片
真的不错,宁波棉服图片 yuiok5dl 真的不错,宁波棉服图片 粘合衬:特点是前片面料及其不可分离部分的厚度比袖.背都厚,且没有加半个上身长度的毛衬.工作服的洗涤工作服的洗涤应尽量采用简易方法进行, ...
- pdf安装包_有么有pdf控件,不需要用户安装任何安装包直接打印的?
如果开发一个软件,需要用到PDF功能,您的选择是基于Adobe PDF吗? 如果是基于Adobe PDF,需要用户安装一个几十M的Adobe的安装包,这显然是不友好的. 即使目前也有了一些其它的阅读器 ...
- 怎么打包图片_超简单的免费批量图片压缩技巧,只需3步
我们在上传图片的时候,经常会遇到一个问题,那就是图片文件太大,无法上传.那这个时候我们该怎么办呢?我们一般都会想到把图片进行压缩之后,重新上传.那么我们要怎么压缩图片呢?如果图片数量很多,能不能进行批 ...
- python pdf处理工具_用Python处理pdf文档
介绍 译者翻译了很多Python强大的包,其中,一以贯之的思想是:面向对象.我用下面的翻译来举一个例子,比如:从PyPDF2包中导入PdfFileReader包.PdfFileReader是此包的一个 ...
最新文章
- 用openCV去除文字中乱入的线条
- 如何用文本档编辑c语言,c语言读写word文档
- datetime模块及time模块
- 协作更进一步:微软隆重介绍Visual Studio动态分享功能
- 多路查找树之2-3树(理论)
- Science报道新研究:同行评审后的研究仅比预印本研究质量提高4%
- Microsoft 宣布 Visual Studio 2019 发布日期
- JS 继承各种方法的优劣比较 ----JS 学习笔记(五)
- 【bzoj4530】[Bjoi2014]大融合 LCT维护子树信息
- 爱因斯坦谜题:谁养鱼(C#版)
- 连续年份高精度人口密度分布数据
- 行业寒冬大浪淘沙,互联网电视如何逆流而上
- Java写后门,JAVA简单编写后门程序
- 机器学习算法——支持向量机SVM4(SMO算法及KTT条件)
- 孟子《生于忧患,死于安乐》日:“故天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,曾益其所不能。”...
- 消费者需求研究能够解决什么问题
- C++ COM组件的编写
- esp32的python教程步数采集_ESP32CAM micropython的操作指南
- mac安装win10_Mac电脑运行Win10翻车,苹果建议更新显卡驱动
- 昆仑天工AIGC——基于Stable Diffusion的多语言AI作画大模型测评
热门文章
- Linux 打包和压缩
- ubuntu 18.04下 配置qt opencv的坑
- poj 3621 Sightseeing Cows 01分数规划
- 《孙子兵法》【火攻第十二】
- python测试c语言代码_numpy C语言源代码调试(一)
- linux系统如何创建python文件_Linux搭建python环境详解
- 与熊论道为什么解码不了_楼上熊孩子瞎蹦跳,楼下邻居投诉无果,一招吓哭“熊一家”...
- wpf checkbox选中触发事件_Web前端开发(16)——JQuery事件绑定与插件
- idea 远程调试_IDEA太强悍,针对调试器和代码分析器的改进,提前知道代码怎么跑...
- php操作xml类,PHP实现的XML操作类【XML Library】