Skip to content

Commit b108bfe

Browse files
committed
mark down support added
1 parent 89ea480 commit b108bfe

File tree

8 files changed

+187
-10
lines changed

8 files changed

+187
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## 1.0.5
2+
* Markdown to pdf support added
13
## 1.0.4
24
* Fix wrong styles of background color ([#31](https://github.com/alihassan143/htmltopdfwidgets/issues/31))
35
* Add support for custom fonts ([#34](https://github.com/alihassan143/htmltopdfwidgets/pull/34)) by @hig-dev

README.md

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
# HTMLtoPDFWidgets
22

33

4-
HTMLtoPDFWidgets is a Flutter package that allows you to convert HTML content into PDF documents with support for various Rich Text Editor formats. With this package, you can effortlessly generate PDF files that include elements such as lists, paragraphs, images, quotes, and headings.
4+
HTMLtoPDFWidgets is a Flutter package that allows you to convert HTML and Markdown content into PDF documents with support for various Rich Text Editor formats. With this package, you can effortlessly generate PDF files that include elements such as lists, paragraphs, images, quotes, and headings.
5+
6+
## Help Maintenance
7+
8+
I've been maintaining quite many repos these days and burning out slowly. If you could help me cheer up, buying me a cup of coffee will make my life really happy and get much energy out of it.
9+
10+
<a href="https://www.buymeacoffee.com/alihassan13" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/purple_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;" ></a>
511

612
## Features
713

814
- Convert HTML content to PDF documents in Flutter apps
9-
- Support for Rich Text Editor formats
15+
- Support for Markdown to PDF conversion
16+
- Rich Text Editor formatting support
1017
- Seamless integration with your Flutter project
1118
- Lightweight and easy to use
1219

@@ -16,7 +23,7 @@ Add the following dependency to your `pubspec.yaml` file:
1623

1724
```yaml
1825
dependencies:
19-
htmltopdfwidgets: ^0.0.9+1
26+
htmltopdfwidgets: ^1.0.5
2027
```
2128
2229
## Usage
@@ -55,6 +62,47 @@ final htmlContent = '''
5562
}));
5663
await file.writeAsBytes(await newpdf.save());
5764
```
65+
2. Converting Markdown to PDF:
66+
67+
```dart
68+
final markDown = '''
69+
# Basic Markdown Demo
70+
---
71+
The Basic Markdown Demo shows the effect of the four Markdown extension sets
72+
on formatting basic and extended Markdown tags.
73+
74+
## Overview
75+
76+
The Dart [markdown](https://pub.dev/packages/markdown) package parses Markdown
77+
into HTML. The flutter_markdown package builds on this package using the
78+
abstract syntax tree generated by the parser to make a tree of widgets instead
79+
of HTML elements.
80+
81+
The markdown package supports the basic block and inline Markdown syntax
82+
specified in the original Markdown implementation as well as a few Markdown
83+
extensions. The markdown package uses extension sets to make extension
84+
management easy. There are four pre-defined extension sets; none, Common Mark,
85+
GitHub Flavored, and GitHub Web. The default extension set used by the
86+
flutter_markdown package is GitHub Flavored.
87+
88+
The Basic Markdown Demo shows the effect each of the pre-defined extension sets
89+
has on a test Markdown document with basic and extended Markdown tags. Use the
90+
Extension Set dropdown menu to select an extension set and view the Markdown
91+
widget's output.
92+
93+
## Comments
94+
95+
Since GitHub Flavored is the default extension set, it is the initial setting
96+
for the formatted Markdown view in the demo.
97+
''';
98+
99+
final List<Widget> markdownWidgets = await HTMLToPdf().convertMarkdown(markDown);
100+
final markdownPdf = Document();
101+
markdownPdf.addPage(MultiPage(
102+
build: (context) => markdownWidgets,
103+
));
104+
await File('markdown_example.pdf').writeAsBytes(await markdownPdf.save());
105+
```
58106

59107
For more details on usage and available options, please refer to the [API documentation](https://pub.dev/documentation/htmltopdfwidgets/latest).
60108

@@ -81,9 +129,5 @@ I use their Html To Document plugin as reference
81129

82130
Happy PDF generation with HTMLtoPDFWidgets in your Flutter apps!
83131

84-
## Help Maintenance
85132

86-
I've been maintaining quite many repos these days and burning out slowly. If you could help me cheer up, buying me a cup of coffee will make my life really happy and get much energy out of it.
87-
88-
<a href="https://www.buymeacoffee.com/alihassan13" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/purple_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;" ></a>
89133

example/lib/main.dart

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,59 @@ const htmlText = '''<h1>AppFlowyEditor</h1>
4848
<input type="checkbox" id="option3">
4949
<label for="option3">Option 3</label>
5050
''';
51+
const String markDown = """
52+
# Basic Markdown Demo
53+
---
54+
The Basic Markdown Demo shows the effect of the four Markdown extension sets
55+
on formatting basic and extended Markdown tags.
5156
57+
## Overview
58+
59+
The Dart [markdown](https://pub.dev/packages/markdown) package parses Markdown
60+
into HTML. The flutter_markdown package builds on this package using the
61+
abstract syntax tree generated by the parser to make a tree of widgets instead
62+
of HTML elements.
63+
64+
The markdown package supports the basic block and inline Markdown syntax
65+
specified in the original Markdown implementation as well as a few Markdown
66+
extensions. The markdown package uses extension sets to make extension
67+
management easy. There are four pre-defined extension sets; none, Common Mark,
68+
GitHub Flavored, and GitHub Web. The default extension set used by the
69+
flutter_markdown package is GitHub Flavored.
70+
71+
The Basic Markdown Demo shows the effect each of the pre-defined extension sets
72+
has on a test Markdown document with basic and extended Markdown tags. Use the
73+
Extension Set dropdown menu to select an extension set and view the Markdown
74+
widget's output.
75+
76+
## Comments
77+
78+
Since GitHub Flavored is the default extension set, it is the initial setting
79+
for the formatted Markdown view in the demo.
80+
""";
5281
createDocument() async {
53-
const filePath = 'example.pdf';
82+
const filePath = 'html_example.pdf';
83+
const markDownfilePath = 'markdown_example.pdf';
5484
final file = File(filePath);
85+
final markdownfile = File(markDownfilePath);
5586
final newpdf = Document();
87+
final markdownNewpdf = Document();
5688
final List<Widget> widgets = await HTMLToPdf().convert(
5789
htmlText,
5890
);
59-
91+
final List<Widget> markdownwidgets = await HTMLToPdf().convertMarkdown(
92+
markDown,
93+
);
6094
newpdf.addPage(MultiPage(
6195
maxPages: 200,
6296
build: (context) {
6397
return widgets;
6498
}));
99+
markdownNewpdf.addPage(MultiPage(
100+
maxPages: 200,
101+
build: (context) {
102+
return markdownwidgets;
103+
}));
65104
await file.writeAsBytes(await newpdf.save());
105+
await markdownfile.writeAsBytes(await markdownNewpdf.save());
66106
}

example/pubspec.lock

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ packages:
99
url: "https://pub.dev"
1010
source: hosted
1111
version: "3.6.1"
12+
args:
13+
dependency: transitive
14+
description:
15+
name: args
16+
sha256: bf9f5caeea8d8fe6721a9c358dd8a5c1947b27f1cfaa18b39c301273594919e6
17+
url: "https://pub.dev"
18+
source: hosted
19+
version: "2.6.0"
1220
async:
1321
dependency: transitive
1422
description:
@@ -112,6 +120,14 @@ packages:
112120
url: "https://pub.dev"
113121
source: hosted
114122
version: "4.0.0"
123+
markdown:
124+
dependency: transitive
125+
description:
126+
name: markdown
127+
sha256: ef2a1298144e3f985cc736b22e0ccdaf188b5b3970648f2d9dc13efd1d9df051
128+
url: "https://pub.dev"
129+
source: hosted
130+
version: "7.2.2"
115131
meta:
116132
dependency: transitive
117133
description:

lib/htmltopdfwidgets.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export 'package:pdf/widgets.dart';
55

66
export 'src/html_to_widgets_codec.dart';
77
export 'src/htmltagstyles.dart';
8+

lib/src/html_to_widgets_codec.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'dart:async';
22

3+
import 'package:markdown/markdown.dart';
4+
35
import '../htmltopdfwidgets.dart';
46
import 'html_to_widgets.dart';
57

@@ -31,6 +33,29 @@ class HTMLToPdf extends HtmlCodec {
3133
//convert function that convert string to dom nodes that that dom nodes will be converted
3234
return await widgetDecoder.convert(html);
3335
}
36+
37+
@override
38+
Future<List<Widget>> convertMarkdown(String markDown,
39+
{List<Font> fontFallback = const [],
40+
//font resolver (font name, bold, italic) => font
41+
FutureOr<Font> Function(String, bool, bool)? fontResolver,
42+
String defaultFontFamily = "Roboto",
43+
double defaultFontSize = 12.0,
44+
//custom html tag styles
45+
HtmlTagStyle tagStyle = const HtmlTagStyle()}) async {
46+
// TODO: implement convertMarkdown
47+
final widgetDecoder = WidgetsHTMLDecoder(
48+
//font fall back if provided
49+
fontFallback: [...fontFallback],
50+
fontResolver: fontResolver,
51+
defaultFontFamily: defaultFontFamily,
52+
defaultFontSize: defaultFontSize,
53+
//custom html tags style
54+
customStyles: tagStyle);
55+
final html = markdownToHtml(markDown);
56+
//convert function that convert string to dom nodes that that dom nodes will be converted
57+
return await widgetDecoder.convert(html);
58+
}
3459
}
3560

3661
// Define an abstract class named HtmlCodec.
@@ -45,4 +70,8 @@ abstract class HtmlCodec {
4570
{List<Font> fontFallback = const [],
4671
FutureOr<Font> Function(String, bool, bool)? fontResolver,
4772
HtmlTagStyle tagStyle = const HtmlTagStyle()});
73+
Future<List<Widget>> convertMarkdown(String markDown,
74+
{List<Font> fontFallback = const [],
75+
FutureOr<Font> Function(String, bool, bool)? fontResolver,
76+
HtmlTagStyle tagStyle = const HtmlTagStyle()});
4877
}

pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: htmltopdfwidgets
22
description: Htmlt to pdf widgets library convert html text to pdf widgets
3-
version: 1.0.4
3+
version: 1.0.5
44
homepage: https://github.com/alihassan143/htmltopdfwidgets
55

66
environment:
@@ -10,6 +10,7 @@ dependencies:
1010
pdf: ">=3.11.1 <4.0.0"
1111
html: ">=0.15.4 <1.0.0"
1212
http: ">=1.2.2 <2.0.0"
13+
markdown: ^7.2.2
1314

1415
dev_dependencies:
1516
flutter_lints: ^4.0.0

test/htmltopdfwidgets_test.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import 'package:htmltopdfwidgets/htmltopdfwidgets.dart';
2020
import 'package:test/test.dart';
2121

2222
late Document pdf;
23+
late Document marDownPdf;
2324

2425
void main() {
2526
const htmlText = ''' <h1>Heading Example</h1>
@@ -52,6 +53,7 @@ void main() {
5253
Document.debug = true;
5354
RichText.debug = true;
5455
pdf = Document();
56+
marDownPdf = Document();
5557
});
5658

5759
test('convertion_test', () async {
@@ -63,8 +65,50 @@ void main() {
6365
}));
6466
});
6567

68+
const String markDown = """
69+
# Basic Markdown Demo
70+
---
71+
The Basic Markdown Demo shows the effect of the four Markdown extension sets
72+
on formatting basic and extended Markdown tags.
73+
74+
## Overview
75+
76+
The Dart [markdown](https://pub.dev/packages/markdown) package parses Markdown
77+
into HTML. The flutter_markdown package builds on this package using the
78+
abstract syntax tree generated by the parser to make a tree of widgets instead
79+
of HTML elements.
80+
81+
The markdown package supports the basic block and inline Markdown syntax
82+
specified in the original Markdown implementation as well as a few Markdown
83+
extensions. The markdown package uses extension sets to make extension
84+
management easy. There are four pre-defined extension sets; none, Common Mark,
85+
GitHub Flavored, and GitHub Web. The default extension set used by the
86+
flutter_markdown package is GitHub Flavored.
87+
88+
The Basic Markdown Demo shows the effect each of the pre-defined extension sets
89+
has on a test Markdown document with basic and extended Markdown tags. Use the
90+
Extension Set dropdown menu to select an extension set and view the Markdown
91+
widget's output.
92+
93+
## Comments
94+
95+
Since GitHub Flavored is the default extension set, it is the initial setting
96+
for the formatted Markdown view in the demo.
97+
""";
98+
99+
test('markdown_convertion_test', () async {
100+
List<Widget> widgets = await HTMLToPdf().convertMarkdown(markDown);
101+
marDownPdf.addPage(MultiPage(
102+
maxPages: 200,
103+
build: (context) {
104+
return widgets;
105+
}));
106+
});
107+
66108
tearDownAll(() async {
67109
final file = File('example.pdf');
68110
await file.writeAsBytes(await pdf.save());
111+
final markDownfile = File('markdown_example.pdf');
112+
await markDownfile.writeAsBytes(await marDownPdf.save());
69113
});
70114
}

0 commit comments

Comments
 (0)