【问题标题】:flutter create pdf using syncfusion颤振使用syncfusion创建pdf
【发布时间】:2021-08-30 11:13:21
【问题描述】:

你好,我创建了一个颤振应用程序,我正在使用这个包创建一个 pdf https://pub.dev/packages/syncfusion_flutter_pdf

我不知道在此处添加 pdf,所以我附上驱动器链接以下载或查看 pdf

pdf 或我想要的 pdf 格式在下面,你可以从下面的链接下载它

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

到目前为止,我已经创建了下面的 pdf,你可以从下面的链接下载它

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

我只是想从这个 pdf https://drive.google.com/file/d/1WR1U7fYmBn9Pm1FyXdXfFxEvVhKTU3mw/view?usp=sharing 创建或附加格式

这是我尝试创建 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? shreennansharda@gmail.com 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));
  }

基本上在我的应用程序中,当用户单击下载按钮时,数据应该以第一个链接中给出的格式下载。

我尝试创建格式,但有点困难,所以请帮我解决这个问题!

请帮忙!!

提前致谢!!!!

更新问题

我尝试了一些方法,但无法将网格添加到第三列,并且页脚中的数量仅出现在第一页上

这里是我试过的 pdf

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

请帮忙!!!

【问题讨论】:

    标签: flutter pdf syncfusion


    【解决方案1】:

    我们创建了一个简单的示例来创建您之前共享的 PDF 文档。请在下面找到代码示例,

      //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;
    }
    

    请从https://www.syncfusion.com/downloads/support/directtrac/general/ze/create_pdf_file-1346255899找到样品

    还可以从https://www.syncfusion.com/downloads/support/directtrac/general/ze/samples-74335454 找到输出的 PDF 样本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-07
      • 2022-07-17
      • 2020-09-15
      • 1970-01-01
      • 2021-12-09
      • 2021-10-08
      • 1970-01-01
      • 2021-08-02
      相关资源
      最近更新 更多