我一直在使用TPPDF来生成利用表格的pdf。使用旧版本,我可以按照下面的代码填充、设置对齐方式和样式。
_bodyData数组由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
}在新版本中,我已经看到了像这样的例子,TPPDF Table Alignments如何将字符串、整数、图像混合到表数据中,当它显示表内容接受以下形式的数据时。
let tableData: [[String]] = [...] // This is your nested data如果能帮上忙,我将不胜感激。
发布于 2020-11-21 17:03:14
generateCells()的接口已经被弃用了一段时间,并在最近的更新中被删除。
相反,请看一下Table Example,它展示了如何使用更高级的API。下面是摘录:
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
]我希望这会有所帮助,否则请仔细看看documentation。
致以最好的问候,Phil (TPPDF的创建者)
https://stackoverflow.com/questions/64127381
复制相似问题