fbpx
Lección 1, Tema 1
En Progreso

Como usar listas en documentos PDF

Jorge L. febrero 22, 2024

Codigo

Dependencia

printing: ^5.9.1
pdf: ^3.8.1
file_picker: ^4.6.1

// Automatic FlutterFlow imports
import '../../backend/backend.dart';
import '../../flutter_flow/flutter_flow_theme.dart';
import '../../flutter_flow/flutter_flow_util.dart';
import '../actions/index.dart'; // Imports other custom actions
import '../../flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';
import 'dart:io';
import 'package:file_picker/file_picker.dart';

Future generatePDF(
  String pdfName,
  List<ItemRecord> products,
  int invoiceNumber,
  double totalAmount,
) async {
  // Add your function code here!
  final pdf = pw.Document();

  // Add a title page
  pdf.addPage(
    pw.Page(
      build: (pw.Context context) {
        return pw.Column(
          children: [
            pw.Text('Invoice', style: pw.TextStyle(fontSize: 32)),
            pw.SizedBox(height: 16),
            pw.Text('Invoice number: $invoiceNumber',
                style: pw.TextStyle(fontSize: 16)),
            pw.SizedBox(height: 32),
            pw.Text('Product list:', style: pw.TextStyle(fontSize: 24)),
            pw.SizedBox(height: 16),
            pw.Table.fromTextArray(
              context: context,
              data: [
                ['Product name', 'Quantity', 'Price'],
                ...products.map((product) => [
                      product.name,
                      product.quantity.toString(),
                      product.price.toString(),
                    ]),
              ],
            ),
            pw.SizedBox(height: 32),
            pw.Text('Total: ${totalAmount.toString()}',
                style: pw.TextStyle(fontSize: 16)),
          ],
        );
      },
    ),
  );

  await Printing.layoutPdf(
      onLayout: (PdfPageFormat format) async => pdf.save());
}

¿Cómo de útil te ha parecido esta clase?