Print math equations on paper through flutter web

  Kiến thức lập trình

Im developing a web app using flutter which shows mcqs. Mcqs might be in simple text or in equations. Im using firebase to i have textual data which i convert using flutter_tex package to show equations which works fine.
I want to create a feature which print those mcqs on a4 paper but the issue is im successfully create and store plain text in pdf and download it but the equations are not tricky to print.

`Future<void> generatePDF(List<Map<String, dynamic>> mcqsList) async {
    final pdf = pw.Document();

    // Add MCQs to PDF
    for (final mcqData in mcqsList) {
      pdf.addPage(
        pw.Page(
          build: (pw.Context context) {
            return pw.SizedBox(
              height: 500,
              child: pw.Column(
                crossAxisAlignment: pw.CrossAxisAlignment.start,
                children: [
                  // Question
                  pw.Text(
                    mcqData['question'],
                    style: pw.TextStyle(fontSize: 18),
                  ),


                  pw.Column(
                    crossAxisAlignment: pw.CrossAxisAlignment.start,
                    children: [
                      for (final option in ['A', 'B', 'C', 'D'])
                        pw.Row(
                          children: [
                            pw.Text(
                              '$option: ',
                              style: pw.TextStyle(fontWeight: pw.FontWeight.bold),
                            ),
                            if (mcqData[option] is String && mcqData[option].contains(r'('))
                              SizedBox(
                                // Wrap TeXView with Container
                                child: TeXLayout(
                                  teXHTML: mcqData[option],
                                ),
                              ),
                            if (mcqData[option] is String && !mcqData[option].contains(r'('))
                              pw.Text(
                                mcqData[option],
                                style: pw.TextStyle(fontSize: 16),
                              ),
                          ],
                        ),
                    ],
                  ),
                ],
              )
            );
          },
        ),
      );
    }


    // Save PDF to bytes
    final bytes = await pdf.save();

    // Convert bytes to Blob
    final blob = html.Blob([bytes], 'application/pdf');

    // Create download link
    final url = html.Url.createObjectUrlFromBlob(blob);
    final anchor = html.AnchorElement(href: url)
      ..setAttribute('download', 'mcqs.pdf')
      ..click();

    // Clean up
    html.Url.revokeObjectUrl(url);
    // Do something with the file (e.g., print or share)
  }`

im trying to print equations on a4 paper through flutter web app and im expecting a solution for this

LEAVE A COMMENT