Skip to content

Feat 314 translate generate pdf to dart #328

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

Open
wants to merge 5 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
30 changes: 30 additions & 0 deletions dart/generate_pdf/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 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

# Directory used by Appwrite CLI for local development
.appwrite
31 changes: 31 additions & 0 deletions dart/generate_pdf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 📄 Dart Generate PDF Function

Generates a sample invoice in PDF.

## 🧰 Usage

### GET /

Returns a PDF invoice.

No parameters required.

**Response**

Sample `200` Response:

Returns a binary stream of the generated PDF document. The `Content-Type` of the response will be set as `application/pdf`.

## ⚙️ Configuration

| Setting | Value |
| ----------------- | --------------- |
| Runtime | Dart (3.5.0) |
| 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
23 changes: 23 additions & 0 deletions dart/generate_pdf/lib/faker.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:faker_dart/faker_dart.dart';

Map<String, dynamic> generateFakeOrder() {
final faker = Faker.instance;
List<Map<String, dynamic>> items = List.generate(
faker.datatype.number(min: 1, max: 5),
(_) => {
'description': faker.commerce.productName(),
'quantity': faker.datatype.number(min: 1, max: 10),
'cost': double.parse(faker.commerce.price(symbol: ''))
.toStringAsFixed(2)
});

return {
'id': faker.datatype.uuid(),
'date': faker.date.past(DateTime.now(), rangeInYears: 25).toIso8601String(),
'name': faker.name.fullName(),
'items': items,
'total': items.fold<num>(0, (sum, item) {
return sum + double.parse(item['cost']);
})
};
}
25 changes: 25 additions & 0 deletions dart/generate_pdf/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'dart:convert';
import './pdf.dart';
import './faker.dart';

Future<List<int>> pdfHandler() async {
final fakeOrder = generateFakeOrder();
print(
'Generated fake order: ${JsonEncoder.withIndent(' ').convert(fakeOrder)}');

final pdfBuffer = await createPDF(fakeOrder);
print('PDF created');

return pdfBuffer;
}

Future main(final context) async {
try {
final pdfBuffer = await pdfHandler();
return context.res
.binary(pdfBuffer, 200, {'Content-Type': 'application/pdf'});
} catch (e) {
context.error('Error occurred while generating PDF: $e');
return context.res.text('Error occurred while generating PDF: $e', 500);
}
}
61 changes: 61 additions & 0 deletions dart/generate_pdf/lib/pdf.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import 'dart:typed_data';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;

Future<Uint8List> createPDF(order) async {
final pdf = pw.Document();
final date = DateTime.parse(order['date']);

final String orderList = (order['items'] as List)
.map((item) =>
'${item['description']} x ${item['quantity']} = \$${item['cost']}')
.join('\n');

pdf.addPage(pw.Page(
pageFormat: PdfPageFormat.a4,
margin: pw.EdgeInsets.only(left: 50),
build: (pw.Context context) {
return pw.Stack(children: [
// Invoice title
pw.Positioned(
bottom: 750,
child:
pw.Text('Sample Invoice', style: pw.TextStyle(fontSize: 20))),

// Order date
pw.Positioned(
left: 350,
bottom: 750,
child: pw.Text('${date.month}/${date.day}/${date.year}',
style: pw.TextStyle(fontSize: 15))),

// Name
pw.Positioned(
bottom: 700,
child: pw.Text('Hello, ${order['name']}!',
style: pw.TextStyle(fontSize: 30))),

// Order ID
pw.Positioned(
bottom: 650,
child: pw.Text('Order ID: ${order['id']}',
style: pw.TextStyle(fontSize: 10))),

// Order total
pw.Positioned(
bottom: 600,
child: pw.Text('Total: \$${order['total'].toStringAsFixed(2)}',
style: pw.TextStyle(fontSize: 15))),

// Order item list
pw.Positioned(
top: 841.89 - 565,
child: pw.Text(orderList,
style: pw.TextStyle(fontSize: 15, lineSpacing: 8))),
]);
}));

final Uint8List pdfBytes = await pdf.save();

return Uint8List.fromList(pdfBytes);
}
15 changes: 15 additions & 0 deletions dart/generate_pdf/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: generate_pdf
version: 1.0.0

environment:
sdk: ^3.5.0

dependencies:
dart_appwrite: ^13.0.0
faker_dart: ^0.2.2
pdf: ^3.11.3
shelf: ^1.4.2

dev_dependencies:
lints: ^5.0.0
test: ^1.24.0