Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alihassan143 committed Sep 12, 2024
1 parent 365568c commit 89ea480
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 65 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.0.4
* Fix wrong styles of background color ([#31](https://github.com/alihassan143/htmltopdfwidgets/issues/31))
* Add support for custom fonts ([#34](https://github.com/alihassan143/htmltopdfwidgets/pull/34)) by @hig-dev
## 1.0.3
* Intial support for checkboxes
*([#25](https://github.com/alihassan143/htmltopdfwidgets/issues/25))
Expand Down
54 changes: 15 additions & 39 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
archive:
dependency: transitive
dependency: "direct overridden"
description:
name: archive
sha256: "22600aa1e926be775fa5fe7e6894e7fb3df9efda8891c73f70fb3262399a432d"
sha256: cb6a278ef2dbb298455e1a713bda08524a175630ec643a242c399c932a0a1f7d
url: "https://pub.dev"
source: hosted
version: "3.4.10"
version: "3.6.1"
async:
dependency: transitive
description:
Expand Down Expand Up @@ -41,14 +41,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.18.0"
convert:
dependency: transitive
description:
name: convert
sha256: "0f08b14755d163f6e2134cb58222dd25ea2a2ee8a195e53983d57c075324d592"
url: "https://pub.dev"
source: hosted
version: "3.1.1"
crypto:
dependency: transitive
description:
Expand All @@ -69,10 +61,10 @@ packages:
dependency: "direct dev"
description:
name: flutter_lints
sha256: a25a15ebbdfc33ab1cd26c63a6ee519df92338a9c10f122adda92938253bef04
sha256: "3f41d009ba7172d5ff9be5f6e6e6abb4300e263aab8866d2a0842ed2a70f8f0c"
url: "https://pub.dev"
source: hosted
version: "2.0.3"
version: "4.0.0"
html:
dependency: transitive
description:
Expand All @@ -87,15 +79,15 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.2"
version: "1.0.4"
http:
dependency: transitive
description:
name: http
sha256: a2bbf9d017fcced29139daa8ed2bba4ece450ab222871df93ca9eec6f80c34ba
sha256: b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d348202e0f0c28f9010
url: "https://pub.dev"
source: hosted
version: "1.2.0"
version: "1.2.2"
http_parser:
dependency: transitive
description:
Expand All @@ -112,22 +104,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.1.4"
js:
dependency: transitive
description:
name: js
sha256: "4186c61b32f99e60f011f7160e32c89a758ae9b1d0c6d28e2c02ef0382300e2b"
url: "https://pub.dev"
source: hosted
version: "0.7.0"
lints:
dependency: transitive
description:
name: lints
sha256: "0a217c6c989d21039f1498c3ed9f3ed71b354e69873f13a8dfc3c9fe76f1b452"
sha256: "976c774dd944a42e83e2467f4cc670daef7eed6295b10b36ae8c85bcbf828235"
url: "https://pub.dev"
source: hosted
version: "2.1.1"
version: "4.0.0"
meta:
dependency: transitive
description:
Expand Down Expand Up @@ -156,10 +140,10 @@ packages:
dependency: transitive
description:
name: pdf
sha256: "93cbb2c06de9bab91844550f19896b2373e7a5ce25173995e7e5ec5e1741429d"
sha256: "05df53f8791587402493ac97b9869d3824eccbc77d97855f4545cf72df3cae07"
url: "https://pub.dev"
source: hosted
version: "3.10.7"
version: "3.11.1"
petitparser:
dependency: transitive
description:
Expand All @@ -168,14 +152,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "6.0.2"
pointycastle:
dependency: transitive
description:
name: pointycastle
sha256: "43ac87de6e10afabc85c445745a7b799e04de84cebaa4fd7bf55a5e1e9604d29"
url: "https://pub.dev"
source: hosted
version: "3.7.4"
qr:
dependency: transitive
description:
Expand Down Expand Up @@ -228,10 +204,10 @@ packages:
dependency: transitive
description:
name: web
sha256: "4188706108906f002b3a293509234588823c8c979dc83304e229ff400c996b05"
sha256: d43c1d6b787bf0afad444700ae7f4db8827f701bc61c255ac8d328c6f4d52062
url: "https://pub.dev"
source: hosted
version: "0.4.2"
version: "1.0.0"
xml:
dependency: transitive
description:
Expand All @@ -241,4 +217,4 @@ packages:
source: hosted
version: "6.5.0"
sdks:
dart: ">=3.2.0 <4.0.0"
dart: ">=3.4.0 <4.0.0"
17 changes: 5 additions & 12 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
name: example
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
publish_to: "none" # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1

environment:
sdk: '>=3.0.3 <4.0.0'

sdk: ">=3.0.3 <4.0.0"

dependencies:




htmltopdfwidgets:
path: ../

dependency_overrides:
archive: 3.6.1
dev_dependencies:



flutter_lints: ^2.0.0
flutter_lints: ^4.0.0
36 changes: 36 additions & 0 deletions lib/src/extension/color_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ extension ColorExtension on PdfColor {
String toRgbaString() {
return 'rgba($red, $green, $blue, $alpha)';
}

static PdfColor hexToPdfColor(String hexColor) {
// Remove the leading '#' if it exists
hexColor = hexColor.replaceAll('#', '');

// Ensure the hex string is in the correct format (6 characters long)
if (hexColor.length == 3) {
hexColor = hexColor.split('').map((char) => '$char$char').join();
}

if (hexColor.length != 6) {
throw ArgumentError('Invalid hex color format');
}

// Convert hex string to integer values for RGB
final int red = int.parse(hexColor.substring(0, 2), radix: 16);
final int green = int.parse(hexColor.substring(2, 4), radix: 16);
final int blue = int.parse(hexColor.substring(4, 6), radix: 16);

// Return a PdfColor object using the RGB values (normalized to 0-1)
return PdfColor.fromInt((red << 16) | (green << 8) | blue);
}
}

// Function to calculate the hex representation of an RGBA color.
Expand All @@ -59,3 +81,17 @@ int hexOfRGBA(int r, int g, int b, {double opacity = 1}) {
return int.parse(
'0x${a.toRadixString(16)}${r.toRadixString(16)}${g.toRadixString(16)}${b.toRadixString(16)}');
}

bool isRgba(String color) {
// Regular expression to check if the color is in 'rgba' format
final rgbaRegex = RegExp(r"^rgba?\((\s*\d+\s*,){2,3}\s*\d+(\.\d+)?\s*\)$",
caseSensitive: false);
return rgbaRegex.hasMatch(color);
}

bool isHex(String color) {
// Regular expression to check if the color is in hex format (#RRGGBB or #RGB)
final hexRegex =
RegExp(r"^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$", caseSensitive: false);
return hexRegex.hasMatch(color);
}
6 changes: 4 additions & 2 deletions lib/src/html_to_widgets.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:collection';
import 'dart:convert';
import 'dart:typed_data';

import 'package:html/dom.dart' as dom;
Expand All @@ -15,7 +16,6 @@ import 'html_tags.dart';
import 'pdfwidgets/bullet_list.dart';
import 'pdfwidgets/number_list.dart';
import 'pdfwidgets/quote_widget.dart';
import 'dart:convert';

////html deocoder that deocde html and convert it into pdf widgets
class WidgetsHTMLDecoder {
Expand Down Expand Up @@ -758,7 +758,9 @@ class WidgetsHTMLDecoder {
final backgroundColorStr = cssMap["background-color"];
final backgroundColor = backgroundColorStr == null
? null
: ColorExtension.tryFromRgbaString(backgroundColorStr);
: isHex(backgroundColorStr)
? ColorExtension.hexToPdfColor(backgroundColorStr)
: ColorExtension.tryFromRgbaString(backgroundColorStr);
if (backgroundColor != null) {
style = style.copyWith(color: backgroundColor);
}
Expand Down
19 changes: 7 additions & 12 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
name: htmltopdfwidgets
description: Htmlt to pdf widgets library convert html text to pdf widgets
version: 1.0.3
version: 1.0.4
homepage: https://github.com/alihassan143/htmltopdfwidgets

environment:
sdk: ">=3.0.0 <4.0.0"

dependencies:
pdf: ">=3.10.3 <4.0.0"
html: ">=0.15.3 <1.0.0"
http: ">=0.13.1 <2.0.0"
dependency_overrides:
pdf:
git:
url: https://github.com/DavBfr/dart_pdf.git
path: pdf/
ref: master
pdf: ">=3.11.1 <4.0.0"
html: ">=0.15.4 <1.0.0"
http: ">=1.2.2 <2.0.0"

dev_dependencies:
flutter_lints: ^2.0.0
lints: ^2.1.1
flutter_lints: ^4.0.0
lints: ^4.0.0
test: ">=1.16.0 <2.0.0"

0 comments on commit 89ea480

Please sign in to comment.