Skip to content

Commit

Permalink
Merge pull request #13 from alihassan143/table_tag
Browse files Browse the repository at this point in the history
chore:version change
  • Loading branch information
alihassan143 authored Sep 7, 2023
2 parents 26b7275 + e875345 commit 14d1f95
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.0.8+2

* support for html table tag added
## 0.0.8+1

* update reamdme.md
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Add the following dependency to your `pubspec.yaml` file:

```yaml
dependencies:
htmltopdfwidgets: ^0.0.8+1
htmltopdfwidgets: ^0.0.8+2
```
## Usage
Expand Down
18 changes: 13 additions & 5 deletions lib/src/html_to_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,10 @@ class WidgetsHTMLDecoder {
return attributes.copyWith(decoration: TextDecoration.combine(decoration));
}

//convert table tag into the table pdf widget
Future<Iterable<Widget>> _parseTable(dom.Element element) async {
final List<TableRow> tablenodes = [];

//iterate over html table tag body
for (final data in element.children) {
final rwdata = await _parsetableRows(data);

Expand All @@ -243,9 +244,10 @@ class WidgetsHTMLDecoder {
];
}

//converts html table tag body to table row widgets
Future<List<TableRow>> _parsetableRows(dom.Element element) async {
final List<TableRow> nodes = [];

//iterate over <tr> tag and convert its children to related pdf widget
for (final data in element.children) {
final tabledata = await _parsetableData(data);

Expand All @@ -254,40 +256,46 @@ class WidgetsHTMLDecoder {
return nodes;
}

//parse html data and convert to table row
Future<TableRow> _parsetableData(
dom.Element element,
) async {
final List<Widget> nodes = [];

//iterate over <tr>children
for (final data in element.children) {
if (data.children.isEmpty) {
//if single <th> or<td> tag found
final node = paragraphNode(text: data.text);

nodes.add(node);
} else {
//if nested <p><br> in <tag> found
final newnodes = await _parseTableSpecialNodes(data);

nodes.addAll(newnodes);
}
}

//returns the tale row
return TableRow(
decoration: BoxDecoration(border: Border.all(color: PdfColors.black)),
children: nodes);
}

//parse the nodes and handle theem accordingly
Future<Iterable<Widget>> _parseTableSpecialNodes(dom.Element element) async {
final List<Widget> nodes = [];

//iterate over multiple childrens
if (element.children.isNotEmpty) {
for (final childrens in element.children) {
//parse them according to their widget
nodes.addAll(await _parseTableDataElementsData(childrens));
}
} else {
nodes.addAll(await _parseTableDataElementsData(element));
}
return nodes;
}
//check if children contains the <p> <li> or any other tag

Future<List<Widget>> _parseTableDataElementsData(dom.Element element) async {
final List<Widget> delta = [];
Expand Down
24 changes: 22 additions & 2 deletions lib/src/html_to_widgets_codec.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
import '../htmltopdfwidgets.dart';
import 'html_to_widgets.dart';

// Define a class named HTMLToPdf that extends HtmlCodec. and it contains the converter that convert html string to pdf widgets
class HTMLToPdf extends HtmlCodec {
// Override the convert method from HtmlCodec.
@override
Future<List<Widget>> convert(String html,
{List<Font> fontFallback = const [],
Future<List<Widget>> convert(
//html string that need to be converted
String html,
{
//font fall back
List<Font> fontFallback = const [],
//default font the html string converted widgets
Font? defaultFont,
//custom html tag styles
HtmlTagStyle tagStyle = const HtmlTagStyle()}) async {
//decode that handle all html tags logic
final widgetDecoder = WidgetsHTMLDecoder(
//font fall back if privided
fontFallback: [...fontFallback],
//default font for the tags
font: defaultFont,
//custom html tags style
customStyles: tagStyle);
//convert function that convert string to dom nodes that that dom nodes will be converted
return await widgetDecoder.convert(html);
}
}

// Define an abstract class named HtmlCodec.
abstract class HtmlCodec {
//this code defines a class HTMLToPdf that inherits from HtmlCodec
// and overrides the convert method to convert HTML content into
// a list of pdf widgets using the WidgetsHTMLDecoder class.
// It also defines an abstract class HtmlCodec with an abstract method convert
// that must be implemented by its subclasses. The code is structured for
//handling HTML-to-pdf-widget conversion in a dart or flutter application
Future<List<Widget>> convert(String html,
{List<Font> fontFallback = const [],
Font? defaultFont,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: htmltopdfwidgets
description: Htmlt to pdf widgets library convert html text to pdf widgets
version: 0.0.8+1
version: 0.0.8+2
homepage: https://github.com/alihassan143/htmltopdfwidgets

environment:
Expand Down

0 comments on commit 14d1f95

Please sign in to comment.