html - 我们可以使用颤振将PDF转换为图像以显示文件的缩略图吗?
问题描述
用例:为了在文件列表中显示 PDF 的缩略图。
问题 2:我们可以将 FPF 转换为图像以在列表中显示缩略图吗?
解决方案
使用pdf_render和图像插件。
import 'package:pdf_render/pdf_render.dart';
import 'package:image/image.dart' as imglib;
final doc = await PdfDocument.openFile('abc.pdf');
final pages = doc.pageCount;
List<imglib.Image> images = [];
// get images from all the pages
for (int i = 1; i <= pages; i++) {
var page = await doc.getPage(i);
var imgPDF = await page.render();
var img = await imgPDF.createImageDetached();
var imgBytes = await img.toByteData(format: ImageByteFormat.png);
var libImage = imglib.decodeImage(imgBytes.buffer
.asUint8List(imgBytes.offsetInBytes, imgBytes.lengthInBytes));
images.add(libImage);
}
// stitch images
int totalHeight = 0;
images.forEach((e) {
totalHeight += e.height;
});
int totalWidth = 0;
images.forEach((element) {
totalWidth = totalWidth < element.width ? element.width : totalWidth;
});
final mergedImage = imglib.Image(totalWidth, totalHeight);
int mergedHeight = 0;
images.forEach((element) {
imglib.copyInto(mergedImage, element, dstX: 0, dstY: mergedHeight, blend: false);
mergedHeight += element.height;
});
// Save image as a file
final documentDirectory = await getExternalStorageDirectory();
File imgFile = new File('${documentDirectory.path}/abc.jpg');
new File(imgFile.path).writeAsBytes(imglib.encodeJpg(mergedImage));
推荐阅读
- installation - Inno Setup 中的 FinishedPage 上未显示自定义 TLabel
- python - Python迭代不会改变
- javascript - 从 Angular 上传 imagem Base64 到 S3
- python - 写入 CSV 文件?
- python - tkinter 窗口图标是否可以在程序托盘中具有加载背景,例如 chrome 下载内容时
- mysql - Multiple Joins With Condition
- python - Python:使用 async/await 进行递归
- pine-script - 未来是否有可能划清界限?
- python - 如何在张量流中使用自编码器模型计算新图像的分数以进行异常检测?
- firebase - Firebase 不向 BigQuery 提供 iOS 事件