0

I have been using TPPDF to generate pdfs utilising tables. With the old version I was able to populate, set alignment and styles as per the code below.

The _bodyData [Array] is comprised of [String, String, UIImage as Any, String, UIImage as Any]

static func addTextTableBody(_bodyData: [Array<Any>], _bodyAlign: [Array<PDFTableCellAlignment>], _bodyWidth: Array<CGFloat>, _bodyStyle: [Array<PDFTableCellStyle>], _colHeaderCount: Int, _colHeaderStyle: PDFTableCellStyle, _rowHeaderStyle: PDFTableCellStyle, _contentStyle: PDFTableCellStyle, _altContentStyle: PDFTableCellStyle, _outlineStyle: PDFLineStyle, _padding: CGFloat, formType: Int) ->PDFTable {
    let tableData = PDFTable()
    // Tables can contain Strings, Numbers, Images or nil, in case you need an empty cell. If you add a unknown content type, an error will be thrown and the rendering will stop.
    do {
        try tableData.generateCells(
        data: _bodyData,
        alignments: _bodyAlign)
    } catch PDFError.tableContentInvalid(let value) {
        // In case invalid input is provided, this error will be thrown.
        print("This type of object is not supported as table content: " + String(describing: (type(of: value))))
    } catch {
        // General error handling in case something goes wrong.
        print("Error while creating table: " + error.localizedDescription)
    }

    // The widths of each column is proportional to the total width, set by a value between 0.0 and 1.0, representing percentage.
    tableData.widths = _bodyWidth

    // To speed up table styling, use a default and change it
    let style = PDFTableStyleDefaults.simple
    style.columnHeaderStyle = _colHeaderStyle
    style.rowHeaderStyle = _colHeaderStyle

    style.contentStyle = _colHeaderStyle
    style.alternatingContentStyle = _colHeaderStyle

    style.columnHeaderCount = _colHeaderCount
    style.rowHeaderCount = 0
    style.footerCount = 0

    style.outline = _outlineStyle//PDFLineStyle(type: .full)
    tableData.style = style

    // Set table padding and margin
    tableData.padding = _padding
    tableData.margin = 0.0

    return tableData
}

With the new version I have seen examples like this TPPDF Table Alignments How do I mix strings, ints, images into table data, when it appears the table content accepts data in the following form.

let tableData: [[String]] = [...] // This is your nested data

I would appreciate any help on this please.

thomkas
  • 1
  • 1
  • Hi thomkas, your `bodyData` is of type `[[Any]]`. There should be nothing stopping you following the example code from the link you posted in running `let alignments = bodyData.map { $0.map { _ in PDFTableCellAlignment.right }}`. If your question is about the data type of the array (`[[Any]]`) then it may be worth looking at https://github.com/techprimate/TPPDF/blob/master/Documentation/Usage.md#content. You may need to wrap the elements in a `PDFTableCellContent` wrapper. – ephemer Sep 30 '20 at 09:25
  • Thank you for your reply. I am new to Swift and the wrapper is what I have been struggling with. Do you have any pointers of how to go about this with the datatypes mentioned? – thomkas Oct 01 '20 at 20:33
  • I have never used or heard of the library before, so I can't speak from experience, but looking at https://github.com/techprimate/TPPDF/blob/master/Source/API/Table/Content/PDFTableContent.swift it seems you can just call `let cellContent = PDFTableContent(content: whatever)`. If you need to access that cell's type, you can inspect `cellContent.type`, which is set internally to the type of content you pass it on init. If you provide the implementation of `tableData.generateCells` this may help people answer your question. Or maybe you can rephrase the question to be more specific – ephemer Oct 05 '20 at 11:40

1 Answers1

0

the API of generateCells() has been deprecated for a while and got removed in the most recent update.

Instead please take a look at the Table Example which shows how to use the more advanced API. Here an excerpt:

var table = PDFTable(rows: 3, columns: 2)
table.content = [
    nil, "Name",
    1,   "Waterfall",
    2,   "Fireworks"
]
table.rows.allRowsAlignment = [.center, .left]
table.widths = [
   0.2, 0.8
]

I hop this helps, otherwise take a closer look at the documentation.

Best regards, Phil (Creator of TPPDF)

Phil Niedertscheider
  • 1,068
  • 11
  • 29