'Convert text editor to html and then print to pdf
I'm trying to print a pdf from a ZefyrEditor and I know that there is no such feature at the moment. So I read that the only way is to convert Zefyr markdown to HTML and then from HTML to pdf (see Github issue here), but I can't figure out how. I have a Json data saved in a document field in Firebase Database (snapsh is my DocumentSnapshot), so if the document exists, I decode the Json and return the NotusDocument and the Delta quill markdowsn to my Editor controller. When I press my button I would like to print my pdf, but obviously it gives me an error.
I tried to use the markdown package, but I can't understand how convert my Json data to correct html data.
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:markdown/markdown.dart' as mk;
Future<NotusDocument> _loadDocument(snapsh) async {
testo =mk.markdownToHtml(jsonDecode(await snapsh['referto']));
if(await snapsh.exists) {
final contents =await snapsh['referto'];
return NotusDocument.fromJson(jsonDecode(contents));
}
final Delta delta = Delta()..insert('ciao\n',{'b':true});
return NotusDocument.fromDelta(delta);
}
_loadDocument(value).then((document) {
setState(() {
_controller = ZefyrController(document);
testoHtml = mk.markdownToHtml(_controller.toString());
});
}
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blueGrey.shade900,
title: Text("$dataVis - $nomePaz"),
actions: <Widget>[
IconButton(
icon: Icon(Icons.print),
onPressed: () async {
final pdf = pw.Document();
Directory appDocDirectory = await getApplicationDocumentsDirectory();
pdf.addPage(pw.Page(
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) {
return pw.Center(
child: pw.Text("$testo"),
); // Center
}));
final file = File(appDocDirectory.path+'/example.pdf');
await file.writeAsBytes(pdf.save());
OpenFile.open(file.path);
},
),
Solution 1:[1]
You can use flutter_html_to_pdf plugin to write HTML source to PDF. To do this, you can either use raw HTML source
var htmlContent =
"""
<!DOCTYPE html>
<html>
<body>
Hello
</body>
</html>
""";
var targetPath = "/your/sample/path";
var targetFileName = "example_pdf_file"
var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlContent(
htmlContent, targetPath, targetFileName);
...or load the HTML from file.
var file = File("/sample_path/example.html");
var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlFile(
file, targetPath, targetFileName);
Solution 2:[2]
Click: > Privacy & messaging > GDPR > Messages > [message style] Edit > Site settings > edit > Logo > Add

Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Omatt |
| Solution 2 | Jeremy Caney |
