csv to image / pdf converter written in Swift
Convert Csv into png image.
https://github.com/user-attachments/assets/a9a7847e-7edc-4e28-b918-66de4b992aa3
https://github.com/user-attachments/assets/9b6f5064-ab6e-4897-a0ab-cf5f104e3bbe
Add the following to your Package.swift
file:
.package(url: "https://github.com/fummicc1/csv2img.git", from: "1.9.1"),
Convert Csv into png image.
Add the following to your Package.swift
file:
.product(name: "Csv2Img", package: "csv2img"),
You cloud convert csv into image / pdf in 3 ways.
String
. let rawCsv = """
a,b,c
1,2,3
4,5,6
7,8,9
10,11,12
"""
let csv = Csv.loadFromString(rawCsv)
let image = try await csv.generate(exportType: .png)
Output:
| a | b | c |
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
| 10 | 11 | 12 |
let rawCsv = """
a,b,c
1,2,3
4,5,6
7,8,9
10,11,12
"""
let url = URL(
fileURLWithPath: "/Users/fumiyatanaka/Downloads/sample.csv"
)
rawCsv.data(using: .utf8)?.write(to: url)
// ----- ↑Just prepared for explanation. -----
let csv = Csv.loadFromDisk(url)
let data = try await csv.generate(fontSize: 12, exportType: .png)
Output:
| a | b | c |
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
| 10 | 11 | 12 |
let rawCsv = """
a,b,c
1,2,3
4,5,6
7,8,9
10,11,12
"""
let url = URL(
string: "https://raw.githubusercontent.com/fummicc1/csv2img/main/Fixtures/sample_1.csv"
)
// ----- ↑Just prepared for explanation. -----
let csv = Csv.loadFromNetwork(url)
let data = try await csv.generate(fontSize: 12, exportType: .png)
Output:
| a | b | c |
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
| 10 | 11 | 12 |
A helper library to generate Csv
in Csv2Img library.
Add the following to your Package.swift
file:
.product(name: "CsvBuilder", package: "csv2img"),
CsvComposition
.import Foundation
import Csv2ImgCore
public struct CsvCompositionExample: CsvComposition {
@CsvRows(column: "age")
public var ages: [String]
@CsvRows(column: "name")
public var names: [String]
public init() { }
}
Csv
let composition: CsvCompositionExample = .init()
composition.ages.append(contentsOf: ["98", "99", "100"])
composition.names.append(contentsOf: ["Yamada", "Tanaka", "Sato"])
let csv = try! composition.build()
or you can write different way like the below.
let yamada = Csv.Row(index: 0, values: ["98", "Yamada"])
let tanaka = Csv.Row(index: 1, values: ["99", "Tanaka"])
let sato = Csv.Row(index: 2, values: ["100", "Sato"])
let csv = try! CsvCompositionParser.parse(type: CsvCompositionExample.self, rows: [yamada, tanaka, sato,])
Result |
---|
A command line tool which generates png-image from csv. (Using Csv2Img
library)
Add the following to your Package.swift
file:
.product(name: "Csv2ImgCmd", package: "csv2img"),
Coomand line interface using Csv2Img
library.
If you have a csv file on your computer, you cloud use this flag with --local
, -l
.
./Csv2ImgCmd --local ~/Downloads/sample.csv ./output.png
If you would like to convert csv file on the internet, you cloud use this flag with --network
, -n
.
./Csv2ImgCmd --network \
https://raw.githubusercontent.com/fummicc1/csv2img/main/Sources/Csv2ImgCmd/Resources/sample_1.csv \
output.png
Pull requests, bug reports and feature requests are welcome 🚀