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

Feat dart generate pdf #254

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions dart/generate_pdf/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# See https://www.dartlang.org/guides/libraries/private-files

# Files and directories created by pub
.dart_tool/
.packages
build/
# If you're building an application, you may want to check-in your pubspec.lock
pubspec.lock

# Directory created by dartdoc
# If you don't generate documentation locally you can remove this line.
doc/api/

# dotenv environment variables file
.env*

# Avoid committing generated Javascript files:
*.dart.js
*.info.json # Produced by the --dump-info flag.
*.js # When generated by dart2js. Don't specify *.js if your
# project includes source files written in JavaScript.
*.js_
*.js.deps
*.js.map

.flutter-plugins
.flutter-plugins-dependencies
48 changes: 48 additions & 0 deletions dart/generate_pdf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# ⚡ Dart Starter Function

A simple starter function. Edit `lib/main.dart` to get started and create something awesome! 🚀

## 🧰 Usage

### GET /

- Returns a "Hello, World!" message.

**Response**

Sample `200` Response:

```text
Hello, World!
```

### POST, PUT, PATCH, DELETE /

- Returns a "Learn More" JSON response.

**Response**

Sample `200` Response:

```json
{
"motto": "Build like a team of hundreds_",
"learn": "https://appwrite.io/docs",
"connect": "https://appwrite.io/discord",
"getInspired": "https://builtwith.appwrite.io"
}
```

## ⚙️ Configuration

| Setting | Value |
|-------------------|-----------------|
| Runtime | Dart (2.17) |
| Entrypoint | `lib/main.dart` |
| Build Commands | `dart pub get` |
| Permissions | `any` |
| Timeout (Seconds) | 15 |

## 🔒 Environment Variables

No environment variables required.
1 change: 1 addition & 0 deletions dart/generate_pdf/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:lints/recommended.yaml
38 changes: 38 additions & 0 deletions dart/generate_pdf/lib/fake_order.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'package:faker/faker.dart';
import 'models/models.dart';

class FakeOrder {
final Faker _faker;
FakeOrder() : _faker = Faker();

List<Item> createFakeItems([items = 10]) {
final List<Item> items = [];
for (var i = 0; i < 10; i++) {
final item = Item(
id: _faker.guid.guid(),
name: _faker.lorem.word(),
price: _faker.currency.random.amount((i) => i, 10).first,
);
items.add(item);
}
return items;
}

Order createFakeOrder() {
final items = createFakeItems();
int totalPrice = 0;

for (var item in items) {
totalPrice = totalPrice + item.price;
}

final order = Order(
id: _faker.guid.guid(),
date: _faker.date.dateTime(),
name: _faker.person.name(),
items: createFakeItems(),
total: totalPrice,
);
return order;
}
}
21 changes: 21 additions & 0 deletions dart/generate_pdf/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:generate_pdf/pdf.dart';
import 'fake_order.dart';

Future<dynamic> main(final context) async {
final pdf = PDF();
final order = FakeOrder();
var fakeOrder = order.createFakeOrder();
context.log(fakeOrder.toJson().toString());
final Uint8List? bytes = await pdf.createAndSave(fakeOrder);

if (bytes != null) {
context.log('PDF created.');
context.log(bytes);

return context.res.send(bytes, 200, {'Content-Type': 'application/pdf'});
}

return context.error("Someting gone wrong");
}
23 changes: 23 additions & 0 deletions dart/generate_pdf/lib/models/item.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Item {
String id;
String name;
int price;

Item({
required this.id,
required this.name,
required this.price,
});

factory Item.fromJson(Map<String, dynamic> json) => Item(
id: json["id"],
name: json["name"],
price: json["price"]?.toDouble(),
);

Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"price": price,
};
}
2 changes: 2 additions & 0 deletions dart/generate_pdf/lib/models/models.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'order.dart';
export 'item.dart';
33 changes: 33 additions & 0 deletions dart/generate_pdf/lib/models/order.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'item.dart';

class Order {
String id;
DateTime date;
String name;
List<Item> items;
int total;

Order({
required this.id,
required this.date,
required this.name,
required this.items,
required this.total,
});

factory Order.fromJson(Map<String, dynamic> json) => Order(
id: json["id"],
date: json["date"],
name: json["name"],
items: List<Item>.from(json["items"].map((x) => x)),
total: json["total"]?.toDouble(),
);

Map<String, dynamic> toJson() => {
"id": id,
"date": date,
"name": name,
"items": List<Item>.from(items.map((x) => x)),
"total": total,
};
}
43 changes: 43 additions & 0 deletions dart/generate_pdf/lib/pdf.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'dart:typed_data';
import 'package:pdf/widgets.dart';
import 'models/models.dart';

class PDF {
late Document pdf;
PDF() : pdf = Document();

Future<Uint8List?> createAndSave(Order order) async {
try {
pdf.addPage(
Page(
build: (Context context) => Column(
children: [
Row(children: [
Text("Invoice"),
SizedBox(width: 100),
Text(order.id)
]),
...order.items
.map((e) => Row(children: [
Text(e.id),
SizedBox(width: 50),
Text(e.name),
SizedBox(width: 50),
Text(e.price.toString()),
]))
.toList()
],
),
),
);
return await _save(order.id);
} catch (e) {
return null;
}
}

Future<Uint8List> _save(String name) async {
var x = await pdf.save();
return x;
}
}
13 changes: 13 additions & 0 deletions dart/generate_pdf/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: generate_pdf
version: 1.0.0

environment:
sdk: ^3.0.0

dependencies:
dart_appwrite: ^10.0.0
pdf: ^3.10.4
faker: ^2.1.0

dev_dependencies:
lints: ^2.0.0