1
votes

hello i have created a flutter app and in that i m using this package to create a pdf https://pub.dev/packages/syncfusion_flutter_pdf

i didnt know to add a pdf here so i m attaching a drive link for downloading or viewing the pdf

the pdf or the format i want in my pdf is below u can download it from below link

https://drive.google.com/file/d/1WR1U7fYmBn9Pm1FyXdXfFxEvVhKTU3mw/view?usp=sharing

so far i have created the below pdf u can download it from below link

https://drive.google.com/file/d/1WyyEANBgmDy6RGR82ZgznW4IWhp41th2/view?usp=sharing

i m just trying to create or attach the format from this pdf https://drive.google.com/file/d/1WR1U7fYmBn9Pm1FyXdXfFxEvVhKTU3mw/view?usp=sharing

this is the code i tried to create pdf

 Future<void> generateInvoice() async {
    //Create a PDF document.
    final PdfDocument document = PdfDocument();
    //Add page to the PDF
    final PdfPage page = document.pages.add();
    //Get page client size
    final Size pageSize = page.getClientSize();
    //Generate PDF grid.
    final PdfGrid grid = PdfGrid();
    // //Add invoice footer
    drawFooter(page, pageSize);
    //Secify the columns count to the grid.
    grid.columns.add(count: 6);
    //Create the header row of the grid.
    final PdfGridRow headerRow = grid.headers.add(1)[0];
    //Set style
    headerRow.style.backgroundBrush = PdfSolidBrush(PdfColor(68, 114, 196));
    headerRow.style.textBrush = PdfBrushes.white;
    headerRow.cells[0].value = 'Product Image';
    headerRow.cells[1].value = 'Product Id';
    headerRow.cells[1].stringFormat.alignment = PdfTextAlignment.center;
    headerRow.cells[2].value = 'Product Name';
    headerRow.cells[3].value = 'Metal';
    headerRow.cells[4].value = 'Stone';
    headerRow.cells[5].value = 'Quantity';
    //Add rows
    for(int i = 0; i < _totalItems; i++){
      final PdfGridRow row = grid.rows.add();
      var imageResponse1 = await get(Uri.parse(photoArray[i]));
      row.cells[0].value = PdfBitmap(imageResponse1.bodyBytes.toList());
      row.cells[1].value = array[i];
      row.cells[2].value = nameArray[i];
      row.cells[3].value = metalArray[i];
      row.cells[4].value = stoneArray[i];
      row.cells[5].value = '1';
      grid.rows[i].height = 100;
    }
    grid.columns[2].width = 200;
    for (int i = 0; i < headerRow.cells.count; i++) {
      headerRow.cells[i].style.cellPadding =
          PdfPaddings(bottom: 5, left: 5, right: 5, top: 5);
    }
    //Apply the table built-in style
    grid.applyBuiltInStyle(PdfGridBuiltInStyle.listTable4Accent5);

    grid.draw(
        page: page,
        bounds: Rect.fromLTWH(0, 0, page.getClientSize().width,
            page.getClientSize().height));
    //Save the PDF document
    final List<int> bytes = document.save();
    //Dispose the document.
    document.dispose();
    //Get external storage directory
    Directory directory = (await getExternalStorageDirectory());
    //Get directory path
    String path = directory.path;
    print(path);
    //Create an empty file to write PDF data
    File file = File('$path/Output.pdf');
    //Write PDF data
    await file.writeAsBytes(bytes, flush: true);
    setState(() {
      isApiCallProcess = false;
    });
    //Open the PDF document in mobile
    OpenFile.open('$path/Output.pdf');
  }

  //Draw the invoice footer data.
  void drawFooter(PdfPage page, Size pageSize) {
    final PdfPen linePen =
    PdfPen(PdfColor(142, 170, 219, 255), dashStyle: PdfDashStyle.custom);
    linePen.dashPattern = <double>[3, 3];
    //Draw line
    page.graphics.drawLine(linePen, Offset(0, pageSize.height - 100),
        Offset(pageSize.width, pageSize.height - 100));

    const String footerContent =
    // ignore: leading_newlines_in_multiline_strings
    '''\r\n\r\nAny Questions? [email protected] Or 971529893336''';

    //Added 30 as a margin for the layout
    page.graphics.drawString(
        footerContent, PdfStandardFont(PdfFontFamily.helvetica, 9),
        format: PdfStringFormat(alignment: PdfTextAlignment.right),
        bounds: Rect.fromLTWH(pageSize.width - 30, pageSize.height - 70, 0, 0));
  }

basically in my app when a user click on the download button the data should get download in the format given in the first link.

i tried to create the format but it is a little difficult so please help me to solve this!

please help!!

Thanks in advance!!!!

update question

i tried something but i could not add the grid to the 3rd column and the quantity in footer is coming on first page only

here the pdf i tried

https://drive.google.com/file/d/1sw4cAu3D58ggUZTkI8GIJza06gUMs86E/view?usp=sharing

please help!!!

2

2 Answers

0
votes

Thanks for contacting Syncfusion support.

Currently we are working on sample creation to create PDF as like requested. We will update further details on 17th June 2021.

With Regards, Anand Panchamoorthi

0
votes

Thanks for your patience.

We have created a simple sample to create a PDF document as you shared before. Please find the code example below,

  //Create a PDF document
  final PdfDocument document = PdfDocument();
  //Add a new page
  final PdfPage page = document.pages.add();

  //Create a string format to set text alignment
  final PdfStringFormat format = PdfStringFormat(
      alignment: PdfTextAlignment.center,
      lineAlignment: PdfVerticalAlignment.middle);
  final PdfStringFormat middleFormat =
      PdfStringFormat(lineAlignment: PdfVerticalAlignment.middle);

  //Create padding, borders for PDF grid
  final PdfPaddings padding = PdfPaddings(left: 2);
  final PdfPen linePen = PdfPen(PdfColor(0, 0, 0), width: 2);
  final PdfPen lastRowBorderPen = PdfPen(PdfColor(0, 0, 0), width: 1);
  final PdfBorders borders = PdfBorders(
      left: linePen, top: linePen, bottom: linePen, right: linePen);
  final PdfBorders lastRowBorder = PdfBorders(
      left: linePen,
      top: linePen,
      bottom: lastRowBorderPen,
      right: linePen);

  //Create a new font
  final PdfFont font = PdfStandardFont(PdfFontFamily.helvetica, 9);

  //Drawing the grid as two seperate grids

  //Create a grid
  final PdfGrid headerGrid = PdfGrid();
  //Set font for all cells
  headerGrid.style.font = font;
  //Add columns
  headerGrid.columns.add(count: 3);
  //Set column width
  headerGrid.columns[0].width = 80;
  headerGrid.columns[2].width = 80;
  //Add a row
  final PdfGridRow headerRow1 = headerGrid.rows.add();
  //Set row height
  headerRow1.height = 70;
  //Add cell value and style properties
  headerRow1.cells[0].value = 'COMPANY LOGO (ROUND)';
  headerRow1.cells[0].style.stringFormat = format;
  headerRow1.cells[1].value = 'SHREE NNANSHARDA JEWELLERY (LOGO)';
  headerRow1.cells[1].style.stringFormat = format;
  headerRow1.cells[1].columnSpan = 2;
  final PdfGridRow headerRow2 = headerGrid.rows.add();
  headerRow2.cells[0].value = '';
  headerRow2.cells[0].columnSpan = 3;
  headerRow2.height = 15;
  final PdfGridRow headerRow3 = headerGrid.rows.add();
  headerRow3.cells[0].value = 'PARTY NAME:';
  headerRow3.cells[0].style.stringFormat = middleFormat;
  headerRow3.cells[0].style.cellPadding = padding;
  headerRow3.cells[2].value = 'DATE:';
  headerRow3.cells[2].style.stringFormat = middleFormat;
  headerRow3.cells[2].style.cellPadding = padding;
  final PdfGridRow headerRow4 = headerGrid.rows.add();
  headerRow4.cells[0].value = '';
  headerRow4.cells[0].columnSpan = 3;
  headerRow4.height = 25;
  //Set border for all rows
  for (int i = 0; i < headerGrid.rows.count; i++) {
    final PdfGridRow headerRow = headerGrid.rows[i];
    if (i == headerGrid.rows.count - 1) {
      for (int j = 0; j < headerRow.cells.count; j++) {
        headerRow.cells[j].style.borders = lastRowBorder;
      }
    } else {
      for (int j = 0; j < headerRow.cells.count; j++) {
        headerRow.cells[j].style.borders = borders;
      }
    }
  }
  //Draw grid and get drawn bounds
  final PdfLayoutResult result =
      headerGrid.draw(page: page, bounds: const Rect.fromLTWH(1, 1, 0, 0))!;

  //Create a new grid
  PdfGrid contentGrid = PdfGrid();
  contentGrid.style.font = font;
  contentGrid.columns.add(count: 4);
  //Add grid header
  contentGrid.headers.add(1);
  contentGrid.columns[0].width = 40;
  contentGrid.columns[1].width = 140;
  contentGrid.columns[3].width = 80;
  //Get header and set values
  final PdfGridRow contentHeader = contentGrid.headers[0];
  contentHeader.cells[0].value = 'SR NO';
  contentHeader.cells[0].style.stringFormat = format;
  contentHeader.cells[0].style.borders = borders;
  contentHeader.cells[1].value = 'PRODUCT IMAGE';
  contentHeader.cells[1].style.stringFormat = format;
  contentHeader.cells[1].style.borders = borders;
  contentHeader.cells[2].value = 'PRODUCT DETAILS';
  contentHeader.cells[2].style.stringFormat = format;
  contentHeader.cells[2].style.borders = borders;
  contentHeader.cells[3].value = 'QUANTITY';
  contentHeader.cells[3].style.stringFormat = format;
  contentHeader.cells[3].style.borders = borders;
  //Add content rows
  contentGrid =
      _addContentRow('1', contentGrid, format, middleFormat, padding);
  contentGrid =
      _addContentRow('2', contentGrid, format, middleFormat, padding);
  contentGrid =
      _addContentRow('3', contentGrid, format, middleFormat, padding);
  contentGrid =
      _addContentRow('4', contentGrid, format, middleFormat, padding);
  contentGrid =
      _addContentRow('5', contentGrid, format, middleFormat, padding);
  //Add a new row
  final PdfGridRow totalRow = contentGrid.rows.add();
  totalRow.cells[0].value = 'TOTAL QUANTITY';
  //Set column span
  totalRow.cells[0].columnSpan = 3;
  totalRow.cells[0].style.stringFormat = format;
  totalRow.height = 25;
  //Set borders for all cells in grid
  for (int i = 0; i < contentGrid.rows.count; i++) {
    final PdfGridRow contentRow = contentGrid.rows[i];
    for (int j = 0; j < contentRow.cells.count; j++) {
      contentRow.cells[j].style.borders = borders;
    }
  }
  //Draw content grid based on the bounds calculated in first grid
  contentGrid.draw(
      page: result.page,
      bounds:
          Rect.fromLTWH(1, result.bounds.top + result.bounds.height, 0, 0));

  //Save PDF document
  final List<int> bytes = document.save();
  //Dispose the document
  document.dispose();

  //Get external storage directory
  Directory directory = (await getApplicationDocumentsDirectory())!;
  //Get directory path
  String path = directory.path;
  //Create an empty file to write PDF data
  File file = File('$path/Output.pdf');
  //Write PDF data
  await file.writeAsBytes(bytes, flush: true);
  //Open the PDF document in mobile
  OpenFile.open('$path/Output.pdf');

//Custom method to create content row and set style properties
PdfGrid _addContentRow(String srNo, PdfGrid grid, PdfStringFormat format,
    PdfStringFormat middleFormat, PdfPaddings padding) {
  //Add a row
  final PdfGridRow contentRow1 = grid.rows.add();
  //Set height
  contentRow1.height = 15;
  //Set values and style properties
  contentRow1.cells[0].value = srNo;
  contentRow1.cells[0].style.stringFormat = format;
  //Set row span
  contentRow1.cells[0].rowSpan = 6;
  contentRow1.cells[1].rowSpan = 6;
  contentRow1.cells[2].value = '';
  contentRow1.cells[3].rowSpan = 6;
  final PdfGridRow contentRow2 = grid.rows.add();
  contentRow2.cells[2].value = 'DESIGN NO-';
  contentRow2.cells[2].style.cellPadding = padding;
  contentRow2.cells[2].style.stringFormat = middleFormat;
  final PdfGridRow contentRow3 = grid.rows.add();
  contentRow3.cells[2].value = 'GROSS WEIGHT-';
  contentRow3.cells[2].style.cellPadding = padding;
  contentRow3.cells[2].style.stringFormat = middleFormat;
  final PdfGridRow contentRow4 = grid.rows.add();
  contentRow4.cells[2].value = 'DIAMOND CTS-';
  contentRow4.cells[2].style.cellPadding = padding;
  contentRow4.cells[2].style.stringFormat = middleFormat;
  final PdfGridRow contentRow5 = grid.rows.add();
  contentRow5.cells[2].value = 'GOLD COLOUR-';
  contentRow5.cells[2].style.cellPadding = padding;
  contentRow5.cells[2].style.stringFormat = middleFormat;
  final PdfGridRow contentRow6 = grid.rows.add();
  contentRow6.cells[2].value = '';
  contentRow6.height = 15;
  final PdfGridRow contentRow7 = grid.rows.add();
  contentRow7.cells[0].value = '';
  contentRow7.cells[0].columnSpan = 4;
  contentRow7.height = 5;
  return grid;
}

Please find the sample from https://www.syncfusion.com/downloads/support/directtrac/general/ze/create_pdf_file-1346255899

And also find the output PDF sample from https://www.syncfusion.com/downloads/support/directtrac/general/ze/samples-74335454.

Please let us know if you need any further assistance in this.

With Regards, Anand Panchamoorthi