im正在扑朔迷离地在pdf生成器应用程序上工作,但是当我想将图像添加到pdf时,花费的时间也太长了,我也想知道如何添加多个图像。
我使用3个库,图像选择器-pdf-打印
这是我的代码:
Future getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
final tempDir = await getTemporaryDirectory();
final path = tempDir.path;
int rand = new Math.Random().nextInt(10000000);
imge.Image imagee = imge.decodeImage(image.readAsBytesSync());
imge.Image smallerImage = imge.copyResize(imagee, 500);
setState(() {
var _image = new File('$path/img_$rand.jpg')..writeAsBytesSync(imge.encodeJpg(smallerImage, quality: 85));
_sharePdf(_image);
});
}
PDFDocument _generateDocument(File _image) {
final pdf = new PDFDocument(deflate: zlib.encode);
final page = new PDFPage(pdf, pageFormat: PDFPageFormat.LETTER);
final g = page.getGraphics();
final font = new PDFFont(pdf);
final top = page.pageFormat.height;
print(top);
imge.Image img = imge.decodeImage(_image.readAsBytesSync());
PDFImage image = new PDFImage(
pdf,
image: img.data.buffer.asUint8List(),
width: img.width,
height: img.height);
g.drawImage(image, 100.0, top - 150.0, 80.0, 100.0);
return pdf;
}
void _sharePdf(File _image) {
print("Share ...");
final pdf = _generateDocument(_image);
// Calculate the widget center for iPad sharing popup position
final RenderBox referenceBox =
shareWidget.currentContext.findRenderObject();
final topLeft =
referenceBox.localToGlobal(referenceBox.paintBounds.topLeft);
final bottomRight =
referenceBox.localToGlobal(referenceBox.paintBounds.bottomRight);
final bounds = new Rect.fromPoints(topLeft, bottomRight);
Printing.sharePdf(document: pdf, bounds: bounds);
}
然后我调用函数getImage()。
我选择图像后,需要花费2-3分钟的时间,所有内容都会冻结,然后您可以共享pdf!
答案 0 :(得分:0)
用于iOS和Android的Flutter_html_to_pdf插件,用于从HTML生成PDF文件。
使用:
https://pub.dev/packages/flutter_html_to_pdf
代替:
首先,在您的pubspec.yaml文件中添加flutter_html_to_pdf作为依赖项。
dependencies:
flutter_html_to_pdf: ^0.5.2
现在在Dart代码中,您可以使用:
import 'package:flutter_html_to_pdf/flutter_html_to_pdf.dart';
用法#
var htmlContent =
"""
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td, p {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<h2>PDF Generated with flutter_html_to_pdf plugin</h2>
<table style="width:100%">
<caption>Sample HTML Table</caption>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>100</td>
</tr>
<tr>
<td>February</td>
<td>50</td>
</tr>
</table>
<p>Image loaded from web</p>
<img src="https://i.imgur.com/wxaJsXF.png" alt="web-img">
</body>
</html>
""";
var targetPath = "/your/sample/path";
var targetFileName = "example_pdf_file"
var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlContent(
htmlContent, targetPath, targetFileName);
您还可以传递内部带有HTML内容的File对象作为参数
var file = File("/sample_path/example.html");
var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlFile(
file, targetPath, targetFileName);
甚至只是该文件的路径
var filePath = "/sample_path/example.html";
var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlFilePath(
filePath, targetPath, targetFileName);
图片 如果要将设备中的本地图片添加到HTML,则需要将图片的路径作为src值传递。
<img src="file:///storage/example/your_sample_image.png" alt="web-img">
或者如果要使用图像File对象
<img src="${imageFile.path}" alt="web-img">
对于多张图片,您可以添加多个标签,例如:
<img src="your source" alt="your alternative">
<img src="your source" alt="your alternative">
<img src="your source" alt="your alternative">
<img src="your source" alt="your alternative">
<img src="your source" alt="your alternative">