Flutter HTML转PDF插件flutter_html_to_pdf_updated的使用
Flutter HTML转PDF插件flutter_html_to_pdf_updated的使用
使用
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内容生成PDF文件。它应该可以处理大多数常见的HTML标记。你不需要在targetFileName
中添加.pdf
扩展名,因为该插件只会生成PDF文件,并且会自动添加扩展名。
其他用法
你还可以传递一个包含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">
更多关于Flutter HTML转PDF插件flutter_html_to_pdf_updated的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复