最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to Generate a DOCX Invoice in Flutter? - Stack Overflow

programmeradmin6浏览0评论

I am working on generating invoices in a Flutter app and need to create a DOCX file. Currently, I am using the docx_template: ^0.4.0 package, which requires creating a pre-designed template file (invoice_template.docx) using MS Word and placing it inside assets/templates/invoice_template.docx.

However, I am wondering if this is the only way to generate a DOCX file in Flutter. Unlike PDFs, where we can dynamically create a layout using flutter_pdf or pdf packages, it seems like DOCX generation requires an existing template.

Is there any way to dynamically generate a DOCX file in Flutter without needing a pre-designed template?

Are there any alternative Flutter packages that allow programmatic creation of DOCX files like how we generate PDFs?

Here is an example of my existing PDF generation function for reference:

Future<void> generatePDF(
  double pdfBasic,
  double pdfHra,
  double pdfConveyance,
  double pdfSpecial,
  double pdfOtherAllowance,
  double pdfTotalFixedEarnings,
  double pdfTotalEarningsWithOvertime,
  double pdfBonus,
  double pdfOverTime,
  double pdfTds,
  double pdfOtherDeductions,
  double pdfLeaveDeductions,
  double pdfTotalDeduction,
  double pdfNetPayableSalary,
  String pdfName,
  String pdfDesignation,
  int pdfPaidDays,
  String pdfFormattedMonthYear,
  int pdfDaysInMonth,
  String pdfnextMonth,
  BuildContext context,
  ) async {

print('I am inside the function');


// Show loading indicator
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (context) => AlertDialog(
        content: Row(
        children: [
        CircularProgressIndicator(),
         SizedBox(width: 16),
           Text('Generating PDF...'),
         ],
       ),
      ),
     );


final ByteData fontData =
    await rootBundle.load('assets/fonts/Roboto-VariableFont_wdth,wght.ttf');
final Uint8List fontBytes = fontData.buffer.asUint8List();
final pw.Font customFont = pw.Font.ttf(fontBytes.buffer.asByteData());

// Load Bold Font
final ByteData fontDataBold =
    await rootBundle.load('assets/fonts/Roboto-Bold.ttf');
final Uint8List fontBytesBold = fontDataBold.buffer.asUint8List();
final pw.Font fontBold = pw.Font.ttf(fontBytesBold.buffer.asByteData());

final pdf = pw.Document();

pdf.addPage(
  pw.Page(
    pageFormat: PdfPageFormat.a4,
    build: (pw.Context context) {
      return pw.Column(
        crossAxisAlignment: pw.CrossAxisAlignment.start,
        children: [
          // Title "Salary Slip"
          pw.Center(
              // child: pw.Text(
              //   'Salary Slip ',
              //   style: pw.TextStyle(font: customFont, fontSize: 16, fontWeight: pw.FontWeight.bold),
              // ),
              ),
          pw.SizedBox(height: 20), // Add some space after the title

          // First Row (Stackgeeks and Print Date)
//IntrinsicHeight

          pw.Row(
            crossAxisAlignment: pw.CrossAxisAlignment.start,
            children: [
              pw.Expanded(
                flex: 2,
                child: pw.Container(
                  padding: pw.EdgeInsets.all(8),
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'Stackgeeks Labs Chandigarh',
                    style: pw.TextStyle(font: fontBold, fontSize: 10),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  padding: pw.EdgeInsets.all(8),
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'Print Date: $pdfnextMonth',
                    style: pw.TextStyle(font: fontBold, fontSize: 10),
                  ),
                ),
              ),
            ],
          ),

          // Empty Rows with Two Columns
          pw.Row(
            crossAxisAlignment: pw.CrossAxisAlignment.start,
            children: [
              pw.Expanded(
                flex: 2,
                child: pw.Container(
                  height: 20, // Fixed height for this example
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  height: 20, // Set the same height as the first column
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                ),
              ),
            ],
          ),

          // Third Row with Two Columns
          pw.Row(
            crossAxisAlignment: pw.CrossAxisAlignment.start,
            children: [
              pw.Expanded(
                flex: 2,
                child: pw.Container(
                  padding: pw.EdgeInsets.all(8),
                  height: 60, // Set a fixed height for both columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Column(
                    crossAxisAlignment: pw.CrossAxisAlignment.start,
                    children: [
                      pw.Text(
                        'Name: $pdfName',
                        style: pw.TextStyle(font: fontBold, fontSize: 10),
                      ),
                      pw.SizedBox(
                          height: 4), // Add some spacing between the lines
                      pw.Text(
                        'Designation: $pdfDesignation',
                        style: pw.TextStyle(font: fontBold, fontSize: 10),
                      ),
                    ],
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  padding: pw.EdgeInsets.all(8),
                  height: 60, // Set the same height as the first column
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Column(
                    crossAxisAlignment: pw.CrossAxisAlignment.start,
                    children: [
                      pw.Text(
                        'Month/Year: $pdfFormattedMonthYear',
                        style: pw.TextStyle(font: fontBold, fontSize: 10),
                      ),
                      pw.SizedBox(
                          height: 4), // Add some spacing between the lines
                      pw.Text(
                        'Month Days: $pdfDaysInMonth',
                        style: pw.TextStyle(font: fontBold, fontSize: 10),
                      ),
                      pw.SizedBox(
                          height: 4), // Add some spacing between the lines
                      pw.Text(
                        'Paid Days: $pdfPaidDays',
                        style: pw.TextStyle(font: fontBold, fontSize: 10),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),

          // Fourth Row with Three Columns
          pw.Row(
            crossAxisAlignment: pw.CrossAxisAlignment.center,
            children: [
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'Fixed Earning',
                    style: pw.TextStyle(font: fontBold, fontSize: 10),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'Net Earning',
                    style: pw.TextStyle(font: fontBold, fontSize: 10),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'Deductions',
                    style: pw.TextStyle(font: fontBold, fontSize: 10),
                  ),
                ),
              ),
            ],
          ),

  // Fifth Row with Six Columns
          pw.Row(
            crossAxisAlignment: pw.CrossAxisAlignment.center,
            children: [
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'Basic',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    '$pdfBasic',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'Basic',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    '$pdfBasic',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'TDS',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    '$pdfTds',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
            ],
          ),

     //sixth row
          pw.Row(
            crossAxisAlignment: pw.CrossAxisAlignment.center,
            children: [
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'HRA',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    '$pdfHra',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'HRA',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    '$pdfHra',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'Other Deductions',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    '$pdfOtherDeductions',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
            ],
          ),

I want similar dynamic content generation for DOCX files.

I am working on generating invoices in a Flutter app and need to create a DOCX file. Currently, I am using the docx_template: ^0.4.0 package, which requires creating a pre-designed template file (invoice_template.docx) using MS Word and placing it inside assets/templates/invoice_template.docx.

However, I am wondering if this is the only way to generate a DOCX file in Flutter. Unlike PDFs, where we can dynamically create a layout using flutter_pdf or pdf packages, it seems like DOCX generation requires an existing template.

Is there any way to dynamically generate a DOCX file in Flutter without needing a pre-designed template?

Are there any alternative Flutter packages that allow programmatic creation of DOCX files like how we generate PDFs?

Here is an example of my existing PDF generation function for reference:

Future<void> generatePDF(
  double pdfBasic,
  double pdfHra,
  double pdfConveyance,
  double pdfSpecial,
  double pdfOtherAllowance,
  double pdfTotalFixedEarnings,
  double pdfTotalEarningsWithOvertime,
  double pdfBonus,
  double pdfOverTime,
  double pdfTds,
  double pdfOtherDeductions,
  double pdfLeaveDeductions,
  double pdfTotalDeduction,
  double pdfNetPayableSalary,
  String pdfName,
  String pdfDesignation,
  int pdfPaidDays,
  String pdfFormattedMonthYear,
  int pdfDaysInMonth,
  String pdfnextMonth,
  BuildContext context,
  ) async {

print('I am inside the function');


// Show loading indicator
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (context) => AlertDialog(
        content: Row(
        children: [
        CircularProgressIndicator(),
         SizedBox(width: 16),
           Text('Generating PDF...'),
         ],
       ),
      ),
     );


final ByteData fontData =
    await rootBundle.load('assets/fonts/Roboto-VariableFont_wdth,wght.ttf');
final Uint8List fontBytes = fontData.buffer.asUint8List();
final pw.Font customFont = pw.Font.ttf(fontBytes.buffer.asByteData());

// Load Bold Font
final ByteData fontDataBold =
    await rootBundle.load('assets/fonts/Roboto-Bold.ttf');
final Uint8List fontBytesBold = fontDataBold.buffer.asUint8List();
final pw.Font fontBold = pw.Font.ttf(fontBytesBold.buffer.asByteData());

final pdf = pw.Document();

pdf.addPage(
  pw.Page(
    pageFormat: PdfPageFormat.a4,
    build: (pw.Context context) {
      return pw.Column(
        crossAxisAlignment: pw.CrossAxisAlignment.start,
        children: [
          // Title "Salary Slip"
          pw.Center(
              // child: pw.Text(
              //   'Salary Slip ',
              //   style: pw.TextStyle(font: customFont, fontSize: 16, fontWeight: pw.FontWeight.bold),
              // ),
              ),
          pw.SizedBox(height: 20), // Add some space after the title

          // First Row (Stackgeeks and Print Date)
//IntrinsicHeight

          pw.Row(
            crossAxisAlignment: pw.CrossAxisAlignment.start,
            children: [
              pw.Expanded(
                flex: 2,
                child: pw.Container(
                  padding: pw.EdgeInsets.all(8),
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'Stackgeeks Labs Chandigarh',
                    style: pw.TextStyle(font: fontBold, fontSize: 10),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  padding: pw.EdgeInsets.all(8),
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'Print Date: $pdfnextMonth',
                    style: pw.TextStyle(font: fontBold, fontSize: 10),
                  ),
                ),
              ),
            ],
          ),

          // Empty Rows with Two Columns
          pw.Row(
            crossAxisAlignment: pw.CrossAxisAlignment.start,
            children: [
              pw.Expanded(
                flex: 2,
                child: pw.Container(
                  height: 20, // Fixed height for this example
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  height: 20, // Set the same height as the first column
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                ),
              ),
            ],
          ),

          // Third Row with Two Columns
          pw.Row(
            crossAxisAlignment: pw.CrossAxisAlignment.start,
            children: [
              pw.Expanded(
                flex: 2,
                child: pw.Container(
                  padding: pw.EdgeInsets.all(8),
                  height: 60, // Set a fixed height for both columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Column(
                    crossAxisAlignment: pw.CrossAxisAlignment.start,
                    children: [
                      pw.Text(
                        'Name: $pdfName',
                        style: pw.TextStyle(font: fontBold, fontSize: 10),
                      ),
                      pw.SizedBox(
                          height: 4), // Add some spacing between the lines
                      pw.Text(
                        'Designation: $pdfDesignation',
                        style: pw.TextStyle(font: fontBold, fontSize: 10),
                      ),
                    ],
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  padding: pw.EdgeInsets.all(8),
                  height: 60, // Set the same height as the first column
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Column(
                    crossAxisAlignment: pw.CrossAxisAlignment.start,
                    children: [
                      pw.Text(
                        'Month/Year: $pdfFormattedMonthYear',
                        style: pw.TextStyle(font: fontBold, fontSize: 10),
                      ),
                      pw.SizedBox(
                          height: 4), // Add some spacing between the lines
                      pw.Text(
                        'Month Days: $pdfDaysInMonth',
                        style: pw.TextStyle(font: fontBold, fontSize: 10),
                      ),
                      pw.SizedBox(
                          height: 4), // Add some spacing between the lines
                      pw.Text(
                        'Paid Days: $pdfPaidDays',
                        style: pw.TextStyle(font: fontBold, fontSize: 10),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),

          // Fourth Row with Three Columns
          pw.Row(
            crossAxisAlignment: pw.CrossAxisAlignment.center,
            children: [
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'Fixed Earning',
                    style: pw.TextStyle(font: fontBold, fontSize: 10),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'Net Earning',
                    style: pw.TextStyle(font: fontBold, fontSize: 10),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'Deductions',
                    style: pw.TextStyle(font: fontBold, fontSize: 10),
                  ),
                ),
              ),
            ],
          ),

  // Fifth Row with Six Columns
          pw.Row(
            crossAxisAlignment: pw.CrossAxisAlignment.center,
            children: [
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'Basic',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    '$pdfBasic',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'Basic',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    '$pdfBasic',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'TDS',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    '$pdfTds',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
            ],
          ),

     //sixth row
          pw.Row(
            crossAxisAlignment: pw.CrossAxisAlignment.center,
            children: [
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'HRA',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    '$pdfHra',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'HRA',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    '$pdfHra',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    'Other Deductions',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
              pw.Expanded(
                flex: 1,
                child: pw.Container(
                  alignment: pw.Alignment.center,
                  padding: pw.EdgeInsets.all(8),
                  height: 30, // Set a fixed height for all columns
                  decoration: pw.BoxDecoration(
                    border: pw.Border.all(
                      color: PdfColors.black,
                      width: 1.5,
                    ),
                  ),
                  child: pw.Text(
                    '$pdfOtherDeductions',
                    style: pw.TextStyle(font: customFont, fontSize: 8),
                  ),
                ),
              ),
            ],
          ),

I want similar dynamic content generation for DOCX files.

Share Improve this question edited Mar 19 at 12:16 Frank van Puffelen 601k85 gold badges890 silver badges860 bronze badges asked Mar 19 at 11:54 Aakash thakurAakash thakur 195 bronze badges 2
  • 1 DIY? The docx file actually is a zip archive (rename it to .zip) and contains XML files as content. So you can easily edit your own docx templates,and fill them in. – Joop Eggen Commented Mar 19 at 12:07
  • plese explain in detail? – Aakash thakur Commented Mar 20 at 5:49
Add a comment  | 

1 Answer 1

Reset to default 0

Do It Yoursef - programming required

The .docx format is primarily a .zip with undermore XML. Assume a template.docx:

Create a template to the XML markup used for title, table and so on. Best without spellchecking that would add extra XML. Rename template.docx to template.zip and unpack it. In my Word version I got:

  • (template/)
    • _rels
    • docProps
    • word
      • document.xml
    • [Content_Types].xml

And then document.xml may be altered (keeping valid XML) as desired. XML viewing with a browser allows you to hierarchically browse the XML tree.

发布评论

评论列表(0)

  1. 暂无评论