Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
頁(yè)dom轉(zhuǎn)換成png,jpeg等,并保存到zip
1.html2canvas使用
html2canvas(document.body).then(function(canvas) { document.body.appendChild(canvas); });
簡(jiǎn)單示例:html2canvas(element, [options]);
options具體參數(shù)看這里
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"); });
如果生成的是單張圖片可以直接保存,如果需要生成多張,這個(gè)時(shí)候就需要將生成的圖片打包下載
var zip = new JSZip(); var img = zip.folder('images'); // 新建一個(gè)images目錄 $('.show').each(function (index) { var id = $(this).attr('id'); // 這里的element只能是html dom 使用$(this)會(huì)報(bào)錯(cuò) // { 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"); });
示例代碼點(diǎn)擊這里
喜歡請(qǐng)點(diǎn)擊關(guān)注哦謝謝啦,另外,更多精彩內(nèi)容敬請(qǐng)關(guān)注百戰(zhàn)程序員哦!
ord插入gif動(dòng)圖不動(dòng)?簡(jiǎn)單3步讓它們乖乖動(dòng)起來(lái)!~效果圖先展示一波!~↓↓↓
想知道圖中的狗狗為什么一臉蒙蔽?想要【免費(fèi)領(lǐng)取268份簡(jiǎn)歷模板】?一起看到【文章最后】吧!~↓↓↓
將圖片導(dǎo)入Word文檔中,發(fā)現(xiàn)不動(dòng)??不急~我們進(jìn)行后面簡(jiǎn)單的操作~
修改圖片格式不難,這里和大家分享更快速的方法~
批量設(shè)置圖片相同大小:
(1)圖片之間按回車進(jìn)行分行;
(2)雙擊第一張圖片,右上方【大小】修改【寬度】(一般默認(rèn)鎖定縱橫比,所以高度可以不修改~);
(3)剩余圖片分別單擊選中,按F4鍵,就可以快速批量應(yīng)用相同大小啦!~
設(shè)置圖片位置:同樣利用快捷鍵~【Ctrl+E】快速居中!~然后再用F4鍵~
怎樣快速另存為?圖片要保存為什么格式,才會(huì)在Word里動(dòng)起來(lái)?
最終答案揭曉!~選中全部圖片,按F12鍵一秒喚出【另存為】對(duì)話框!~
【保存類型】選擇【網(wǎng)頁(yè)*.htm、*.html】。
打開(kāi)另存的網(wǎng)頁(yè)格式文件~華麗麗的動(dòng)圖效果如下~↓↓↓
表白美膩的淋淋!?~么么噠!~
哈哈哈原來(lái)那只狗狗一臉蒙蔽是因?yàn)椋褐魅税蜒K丟到雪地里,它找不到哈哈哈哈哈哈哈嗝!~
小哥辛苦搜集的簡(jiǎn)歷模板,炒雞實(shí)用!~這還只是冰山一角!~大家積極點(diǎn)~【評(píng)論區(qū)留下郵箱】免費(fèi)拿走268份完整版!~不要辜負(fù)小哥的一番心意喲~(^U^)ノ~↓↓↓
識(shí)別圖片中的文字,可以使用【捷速OCR文字識(shí)別軟件】~
TML (超文本標(biāo)記語(yǔ)言)是所有瀏覽器都支持的主要網(wǎng)頁(yè)文件格式。它經(jīng)常用于將數(shù)據(jù)和信息顯示為網(wǎng)頁(yè)。在某些情況下,我們可能需要將 HTML 文檔轉(zhuǎn)換為JPG、PNG、TIFF、BMP、GIF等圖像格式。在本文中,我們將學(xué)習(xí)如何將 HTML 轉(zhuǎn)換為 PNG、JPEG、BMP、GIF、或 Python 中的 TIFF 圖像。
Aspose.Words for .NET官方最新版免費(fèi)下載試用,歷史版本下載,在線文檔和幫助文件下載-慧都網(wǎng)
為了將 HTML 轉(zhuǎn)換為圖像格式,我們將使用Aspose.Words for Python API。它是在 Python 應(yīng)用程序中以編程方式讀取和操作各種類型文檔的完整解決方案。它使我們能夠生成、修改、轉(zhuǎn)換、渲染和打印 Microsoft Word(DOC、DOCX、ODT)、PDF和 Web(HTML、Markdown)文檔。
請(qǐng)?jiān)诳刂婆_(tái)中使用以下 pip 命令從PyPI安裝 API :
> pip install aspose-words
我們可以按照以下步驟輕松地將 HTML 文檔轉(zhuǎn)換為 JPG 圖像:
以下代碼示例展示了如何在 Python 中將 HTML 轉(zhuǎn)換為 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 文檔轉(zhuǎn)換為 PNG 圖像:
以下代碼示例展示了如何在 Python 中將 HTML 轉(zhuǎn)換為 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 文檔轉(zhuǎn)換為 BMP 圖像:
以下代碼示例展示了如何在 Python 中將 HTML 轉(zhuǎn)換為 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 文檔轉(zhuǎn)換為 GIF 圖像。但是,我們只需要在步驟 4 中將圖像保存為帶有“.gif”擴(kuò)展名的 GIF。
以下代碼示例展示了如何在 Python 中將 HTML 轉(zhuǎn)換為 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 文檔轉(zhuǎn)換為 TIFF 圖像:
我們還可以按照以下步驟將 HTML 文檔轉(zhuǎn)換為 TIFF 圖像:
以下代碼示例展示了如何在 Python 中將 HTML 文檔轉(zhuǎn)換為 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 字符串動(dòng)態(tài)生成圖像文件:
以下代碼示例展示了如何在 Python 中將 HTML 字符串轉(zhuǎn)換為 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")
在本文中,我們學(xué)習(xí)了如何:
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。