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

Fix image size in PDF post export #234

Merged
merged 1 commit into from
Dec 24, 2024
Merged
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
96 changes: 64 additions & 32 deletions lib/features/notes/data/repositories/export_notes_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import 'dart:io';
import 'package:dairy_app/core/logger/logger.dart';
import 'package:dairy_app/features/notes/domain/repositories/export_notes_repository.dart';
import 'package:dairy_app/features/notes/domain/repositories/notes_repository.dart';
import 'package:delta_markdown/delta_markdown.dart';
import 'package:flutter/services.dart';
import 'package:flutter_html_to_pdf/flutter_html_to_pdf.dart';
import 'package:intl/intl.dart';
import 'package:markdown/markdown.dart';
import 'package:path_provider/path_provider.dart';

final log = printer("ExportNotesRepository");
Expand Down Expand Up @@ -103,9 +101,37 @@ class ExportNotesRepository implements IExportNotesRepository {
// utils

String quillDeltaToHtml(String delta) {
final markdown = deltaToMarkdown(delta);
final html = markdownToHtml(markdown);
return html;
var deltaMap = jsonDecode(delta);
var html = StringBuffer();

for (var op in deltaMap) {
if (op["insert"] is String) {
var text = op["insert"] as String;
var attributes = op["attributes"] as Map<String, dynamic>?;

if (attributes != null) {
if (attributes.containsKey("bold")) {
text = "<strong>$text</strong>";
}
if (attributes.containsKey("italic")) {
text = "<em>$text</em>";
}
// Add more attribute handling as needed
}

html.write(text);
} else if (op["insert"] is Map) {
var insert = op["insert"] as Map<String, dynamic>;
if (insert.containsKey("image")) {
var image = insert["image"] as Map<String, dynamic>;
html.write(
'<img src="${image['src']}" width="${image['width']}" height="${image['height']}" style="max-width: 100%; height: auto;">');
}
// Handle other insert types as needed
}
}

return html.toString();
}

String formatDate(DateTime date) {
Expand All @@ -129,38 +155,44 @@ class ExportNotesRepository implements IExportNotesRepository {
var deltaMap = jsonDecode(delta);

for (Map<String, dynamic> deltaElement in deltaMap) {
// Local images needs to be prefixed with file:///

// Handle image elements
if (deltaElement.containsKey("insert") &&
deltaElement["insert"].runtimeType != String &&
deltaElement["insert"].containsKey("image") &&
!(deltaElement["insert"]["image"] as String).startsWith("http")) {
deltaElement["insert"]["image"] =
"file://" + deltaElement["insert"]["image"];
deltaElement["insert"] is Map &&
deltaElement["insert"].containsKey("image")) {
var imageUrl = deltaElement["insert"]["image"] as String;

// Prefix local images with file:///
if (!imageUrl.startsWith("http")) {
imageUrl = "file://" + imageUrl;
}

// Add width and height attributes to limit image size
deltaElement["insert"]["image"] = {
"src": imageUrl,
"width": "500", // You can adjust this value as needed
"height": "auto" // This maintains the aspect ratio
};
}

// Remove unsupported attributes on text

if (deltaElement.containsKey("insert") &&
deltaElement["insert"].runtimeType == String) {
if (deltaElement.containsKey("attributes")) {
// 1. Remove underline
deltaElement["attributes"].remove("underline");

// 2. Remove Strikethrough
deltaElement["attributes"].remove("strike");

// 3. Remove color
deltaElement["attributes"].remove("color");

// 4. Remove background color
deltaElement["attributes"].remove("background");

// 5. Remove checklist
deltaElement["attributes"].remove("list");

// 6. Remove subscript and superscript
deltaElement["attributes"].remove("script");
deltaElement["insert"] is String &&
deltaElement.containsKey("attributes")) {
var attributes = deltaElement["attributes"] as Map<String, dynamic>;

// Remove specific unsupported attributes
attributes.removeWhere((key, value) => [
"underline",
"strike",
"color",
"background",
"list",
"script"
].contains(key));

// If all attributes are removed, remove the attributes key entirely
if (attributes.isEmpty) {
deltaElement.remove("attributes");
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1144,14 +1144,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.2.0"
markdown:
dependency: "direct main"
description:
name: markdown
sha256: acf35edccc0463a9d7384e437c015a3535772e09714cf60e07eeef3a15870dcd
url: "https://pub.dev"
source: hosted
version: "7.1.1"
matcher:
dependency: transitive
description:
Expand Down
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ dependencies:
intl: ^0.18.0
local_auth: ^2.1.6
logger: ^1.1.0
markdown: ^7.1.1
package_info_plus: ^4.1.0
path_provider: ^2.0.9
permission_handler: ^11.0.1
Expand Down
Loading