页dom转换成png,jpeg等,并保存到zip
1.html2canvas使用
html2canvas(document.body).then(function(canvas) { document.body.appendChild(canvas); });
简单示例:html2canvas(element, [options]);
options具体参数看这里
2.canvas2image使用
有两种使用方式,第一种直接保存
Canvas2Image.saveAsImage(canvasObj, width, height, type) Canvas2Image.saveAsPNG(canvasObj, width, height) Canvas2Image.saveAsJPEG(canvasObj, width, height) Canvas2Image.saveAsGIF(canvasObj, width, height) Canvas2Image.saveAsBMP(canvasObj, width, height)
第二种生成图片资源
Canvas2Image.convertToImage(canvasObj, width, height, type) Canvas2Image.convertToPNG(canvasObj, width, height) Canvas2Image.convertToJPEG(canvasObj, width, height) Canvas2Image.convertToGIF(canvasObj, width, height) Canvas2Image.convertToBMP(canvasObj, width, height)
3.生成png并保存
html2canvas(document.getElementById(id)) .then(function (canvas) { //Canvas2Image.convertToPNG(canvas); Canvas2Image.saveAsPNG(canvas); });
4.打包到zip
zip使用示例:
var zip = new JSZip(); zip.file("Hello.txt", "Hello World\n"); var img = zip.folder("images"); img.file("smile.gif", imgData, {base64: true}); zip.generateAsync({type:"blob"}) .then(function(content) { // see FileSaver.js saveAs(content, "example.zip"); });
如果生成的是单张图片可以直接保存,如果需要生成多张,这个时候就需要将生成的图片打包下载
var zip = new JSZip(); var img = zip.folder('images'); // 新建一个images目录 $('.show').each(function (index) { var id = $(this).attr('id'); // 这里的element只能是html dom 使用$(this)会报错 // { useCORS: true } 如果需要跨域加载图片 var data = html2canvas(document.getElementById(id), { useCORS: true }) .then(function (canvas) { var image = Canvas2Image.convertToPNG(canvas, 1920, 1080); var image_data = $(image).attr('src'); // 生成的image_data是图片链接,不能直接保存为图片,需要做一下截取 return image_data.split('data:image/png;base64,')[1]; }); img.file(id + '.jpg', data, { base64: true }); }) zip.generateAsync({ type: "blob" }) .then(function (content) { saveAs(content, "example.zip"); });
示例代码点击这里
喜欢请点击关注哦谢谢啦,另外,更多精彩内容敬请关注百战程序员哦!
ord插入gif动图不动?简单3步让它们乖乖动起来!~效果图先展示一波!~↓↓↓
想知道图中的狗狗为什么一脸蒙蔽?想要【免费领取268份简历模板】?一起看到【文章最后】吧!~↓↓↓
将图片导入Word文档中,发现不动??不急~我们进行后面简单的操作~
修改图片格式不难,这里和大家分享更快速的方法~
批量设置图片相同大小:
(1)图片之间按回车进行分行;
(2)双击第一张图片,右上方【大小】修改【宽度】(一般默认锁定纵横比,所以高度可以不修改~);
(3)剩余图片分别单击选中,按F4键,就可以快速批量应用相同大小啦!~
设置图片位置:同样利用快捷键~【Ctrl+E】快速居中!~然后再用F4键~
怎样快速另存为?图片要保存为什么格式,才会在Word里动起来?
最终答案揭晓!~选中全部图片,按F12键一秒唤出【另存为】对话框!~
【保存类型】选择【网页*.htm、*.html】。
打开另存的网页格式文件~华丽丽的动图效果如下~↓↓↓
表白美腻的淋淋!❤~么么哒!~
哈哈哈原来那只狗狗一脸蒙蔽是因为:主人把雪块丢到雪地里,它找不到哈哈哈哈哈哈哈嗝!~
小哥辛苦搜集的简历模板,炒鸡实用!~这还只是冰山一角!~大家积极点~【评论区留下邮箱】免费拿走268份完整版!~不要辜负小哥的一番心意哟~(^U^)ノ~↓↓↓
识别图片中的文字,可以使用【捷速OCR文字识别软件】~
TML (超文本标记语言)是所有浏览器都支持的主要网页文件格式。它经常用于将数据和信息显示为网页。在某些情况下,我们可能需要将 HTML 文档转换为JPG、PNG、TIFF、BMP、GIF等图像格式。在本文中,我们将学习如何将 HTML 转换为 PNG、JPEG、BMP、GIF、或 Python 中的 TIFF 图像。
Aspose.Words for .NET官方最新版免费下载试用,历史版本下载,在线文档和帮助文件下载-慧都网
为了将 HTML 转换为图像格式,我们将使用Aspose.Words for Python API。它是在 Python 应用程序中以编程方式读取和操作各种类型文档的完整解决方案。它使我们能够生成、修改、转换、渲染和打印 Microsoft Word(DOC、DOCX、ODT)、PDF和 Web(HTML、Markdown)文档。
请在控制台中使用以下 pip 命令从PyPI安装 API :
> pip install aspose-words
我们可以按照以下步骤轻松地将 HTML 文档转换为 JPG 图像:
以下代码示例展示了如何在 Python 中将 HTML 转换为 JPG 图像。
# This code example demonstrates how to convert HTML document to JPG images.
import aspose.words as aw
# Load an existing Word document
doc = aw.Document("C:\\Files\\sample.html")
# Specify image save options
# Set save format as JPEG
imageOptions = aw.saving.ImageSaveOptions(aw.SaveFormat.JPEG)
# Set the "JpegQuality" property to "10" to use stronger compression when rendering the document.
# This will reduce the file size of the document, but the image will display more prominent compression artifacts.
imageOptions.jpeg_quality = 10
# Change the horizontal resolution.
# The default value for these properties is 96.0, for a resolution of 96dpi.
# Similarly, change vertical resolution by setting vertical_resolution
imageOptions.horizontal_resolution = 72
# Save the pages as JPG
for page in range(0, doc.page_count):
extractedPage = doc.extract_pages(page, 1)
extractedPage.save(f"C:\\Files\\Images\\Page_{page + 1}.jpg", imageOptions)
我们可以按照以下步骤将 HTML 文档转换为 PNG 图像:
以下代码示例展示了如何在 Python 中将 HTML 转换为 PNG 图像。
# This code example demonstrates how to convert HTML document to PNG images.
import aspose.words as aw
# Load an existing Word document
doc = aw.Document("C:\\Files\\sample.html")
# Specify image save options
# Set save format as PNG
imageOptions = aw.saving.ImageSaveOptions(aw.SaveFormat.PNG)
# Change the image's brightness and contrast.
# Both are on a 0-1 scale and are at 0.5 by default.
imageOptions.image_brightness = 0.3
imageOptions.image_contrast = 0.7
# Save the pages as PNG
for page in range(0, doc.page_count):
extractedPage = doc.extract_pages(page, 1)
extractedPage.save(f"C:\\Files\\Images\\Page_{page + 1}.png", imageOptions)
我们可以按照以下步骤将 HTML 文档转换为 BMP 图像:
以下代码示例展示了如何在 Python 中将 HTML 转换为 BMP 图像。
# This code example demonstrates how to convert HTML document to BMP images.
import aspose.words as aw
# Load an existing Word document
doc = aw.Document("C:\\Files\\sample.html")
# Save the pages as BMP
for page in range(0, doc.page_count):
extractedPage = doc.extract_pages(page, 1)
extractedPage.save(f"C:\\Files\\Images\\Page_{page + 1}.bmp")
同样,我们也可以按照前面提到的步骤将 HTML 文档转换为 GIF 图像。但是,我们只需要在步骤 4 中将图像保存为带有“.gif”扩展名的 GIF。
以下代码示例展示了如何在 Python 中将 HTML 转换为 GIF 图像。
# This code example demonstrates how to convert HTML document to GIF images.
import aspose.words as aw
# Load an existing Word document
doc = aw.Document("C:\\Files\\sample.html")
# Save the pages as GIF
for page in range(0, doc.page_count):
extractedPage = doc.extract_pages(page, 1)
extractedPage.save(f"C:\\Files\\Images\\Page_{page + 1}.gif")
我们还可以按照以下步骤将 HTML 文档转换为 TIFF 图像:
我们还可以按照以下步骤将 HTML 文档转换为 TIFF 图像:
以下代码示例展示了如何在 Python 中将 HTML 文档转换为 TIFF 图像。
# This code example demonstrates how to convert HTML document to TIFF images.
import aspose.words as aw
# Load an existing Word document
doc = aw.Document("C:\\Files\\sample.html")
# Save the document as TIFF
doc.save(f"C:\\Files\\Images\\Output.tiff")
我们可以按照以下步骤从 HTML 字符串动态生成图像文件:
以下代码示例展示了如何在 Python 中将 HTML 字符串转换为 JPG 图像。
# This code example demonstrates how to convert HTML string to an image.
import aspose.words as aw
# Create document object
doc = aw.Document()
# Create a document builder object
builder = aw.DocumentBuilder(doc)
# Insert HTML
builder.insert_html("<ul>\r\n" +
"<li>Item1</li>\r\n" +
"<li>Item2</li>\r\n" +
"</ul>")
# Save the document as JPG
doc.save(f"C:\\Files\\Output.jpg")
在本文中,我们学习了如何:
*请认真填写需求信息,我们会在24小时内与您取得联系。