Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with generateCells, setCellStyle and PDFGenerator.generate #269

Closed
pura-agency opened this issue Apr 8, 2021 · 3 comments
Closed

Comments

@pura-agency
Copy link

pura-agency commented Apr 8, 2021

ℹ Please fill out this template when filing an issue.
All lines beginning with an ℹ symbol instruct you with what info we expect. You can delete those lines once you've filled in the info.

Per our *COMMUNICATION guidelines, we use GitHub for
bugs and feature requests, not general support. Other issues should be opened on Stack Overflow with the tag TPPDF.

What did you do?

ℹ I updated the library to the last version, same error with CocoaPods or SwiftPackage.

What did you expect to happen?

ℹ Using the methods like the previous versions.

What happened instead?

ℹ Error with those methods and I can't find them in the new version of the library code, for example in the old version I can find .generateCells in "PDFTable.swift line 54", .setCellStyle in "PDFTable.swift line 75", PDFGenerator.generate in "PDFGenerator+Generation.swift line 84" (Bundle version string (short) 1.5.4 - found in TPPDF-Info.plist).
I see in your documentation that you suggest these methods even in the new version: https://github.com/techprimate/TPPDF/tree/swift-4.2
The errors are:
• Value of type 'PDFTable' has no member 'generateCells' - for "try table.generateCells(data: cellsData, alignments: alignments)"
• Value of type 'PDFTable' has no member 'setCellStyle' - for "try table.setCellStyle(row: 0, column: 0, style: headerCell)"
• Extra arguments at positions #1, #3, #4 in cal - for "try PDFGenerator.generate(document: document, to: docURL, progress: { progress in }, debug: false)}, debug: false)"
Each of them are in a do try catch or do try statement

TPPDF Environment

TPPDF version: ℹ2.3.4
Xcode version: ℹ12.4
Swift version: ℹ5 - SwiftUI

Demo Code / Project

ℹ Please add a code snippet, link to or upload a project we can download that reproduces the issue.
ℹ We need a fully compilingPDFDocument

do {
try table.generateCells(data: cellsData, alignments: alignments)
} catch PDFError.tableContentInvalid(let value) {
// In case invalid input is provided, this error will be thrown.
//ProgressHUD.showError("Qualcosa è andato storto")
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.
//ProgressHUD.showError("Qualcosa è andato storto")
print("Error while creating table: " + error.localizedDescription)
}

do {
// Style each cell individually
try table.setCellStyle(row: 0, column: 0, style: headerCell)
try table.setCellStyle(row: 0, column: 1, style: headerCell)
try table.setCellStyle(row: 0, column: 2, style: headerCell)
try table.setCellStyle(row: 0, column: 3, style: headerCell)
try table.setCellStyle(row: 0, column: 4, style: headerCell)
try table.setCellStyle(row: 0, column: 5, style: headerCell)
try table.setCellStyle(row: 0, column: 6, style: headerCell)
try table.setCellStyle(row: 0, column: 7, style: headerCell)
} catch PDFError.tableIndexOutOfBounds(let index, let length){
// In case the index is out of bounds

                print("Requested cell is out of bounds! \(index) / \(length)")
            } catch {
                // General error handling in case something goes wrong.

                print("Error while setting cell style: " + error.localizedDescription)
            }

do {

                try PDFGenerator.generate(document: document, to: docURL, progress: { progress in
                    print(progress)
                    //self.popUpPdfProgressView.setProgress(Float(progress), animated: true)

                }, debug: false)

                let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
                let url = NSURL(fileURLWithPath: path)
                if let pathComponent = url.appendingPathComponent("report.pdf") {
                    let filePath = pathComponent.path
                    let fileManager = FileManager.default
                    if fileManager.fileExists(atPath: filePath) {
                        print("FILE AVAILABLE")

                        let controller = UIDocumentInteractionController(url: pathComponent as URL)
                        controller.presentPreview(animated: true)
                    } else {
                        print("FILE NOT AVAILABLE")
                    }
                } else {
                    print("FILE PATH NOT AVAILABLE")
                }

            } catch {
                print("Error while generating PDF: " + error.localizedDescription)
            }
@philprime
Copy link
Member

Hi, thanks for filing an issue.

It seems like you missed a few updates, so I'll quickly summarize them:

  • You are using 1.5.4, after 1.6.0 the table implementation changed drastically, therefore I had to release a new major version 2.0.0 (May 19th 2020)
  • In 2.0.1 I deprecated table.generateCells due to the new table subscript API
  • The "in the new version" branch you are referencing, is the Swift-4.2 branch, which is fairly outdated (last update in April 2019) and not the latest master branch

So in case you want to use version 2.X.X, I recommend you update to the latest table syntax 😃
It should be worth the changes!

@pura-agency
Copy link
Author

pura-agency commented Apr 10, 2021

Thank you I'm update the code of an outdated project and i was looking in the wrong page for the documentation, I like the changes but when I try to access the row I'm having a fatal error - Index out of range

let table = PDFTable()
table.showHeadersOnEveryPage = false
table.padding = 5.0
table.widths = [
                0.15, 0.2, 0.15, 0.1, 0.1, 0.1, 0.1, 0.1
            ]
let row = table[row: 0]
row.content = ["Text1", "Text2", "Text3", "Text4", "Text5", "Text6.", "Text7", "Text8"]
row.alignment = [.left, .left, .left, .left, .left, .left, .center, .center]

@philprime
Copy link
Member

Thanks for pointing this out. In fact, this is a documentation error.

Excerpt from PDFTable.swift:

    public convenience init(size: (rows: Int, columns: Int) = (0, 0)) {
        self.init(rows: size.rows, columns: size.columns)
    }

    /**
     Creates a new table with the given size and populates it with empty cells.
     */
    public init(rows: Int = 0, columns: Int = 0) {
        self.size = (rows: rows, columns: columns)
        ...
    }

As you can see, it is necessary to provide a table size, so the subscript accessors work properly. I changed your example a little bit and it works now:

let table = PDFTable(rows: 1, columns: 8)
table.showHeadersOnEveryPage = false
...

I will furthermore remove the default size (0,0) and fix the documentation accordingly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants