Markdown2PDF
A Flutter package for converting markdown content from HTTP responses to PDF with proper formatting.
Features
- π Convert markdown from HTTP URLs to PDF
- π Convert markdown from strings to PDF
- π¨ Beautiful PDF formatting with customizable styles
- π Support for tables, code blocks, lists, and more
- π HTTP response handling with timeout and headers
- π± Share and print PDFs directly
- βοΈ Highly customizable PDF options
- π§ͺ Well-tested with comprehensive test coverage
Installation
Add this to your package's pubspec.yaml file:
dependencies:
markdown_2_pdf: ^1.0.0
Then run:
flutter pub get
Usage
Basic Usage
import 'package:markdown_2_pdf/markdown_2_pdf.dart';
// Convert from URL
final source = HttpMarkdownSource(url: 'https://example.com/README.md');
final converter = MarkdownToPdfConverter();
// Save to file
final file = await converter.convertToFile(source, fileName: 'document.pdf');
// Or share directly
await converter.convertAndShare(source);
Convert from String
final markdownContent = '''
# My Document
This is a **bold** text and this is *italic*.
## Features
- Feature 1
- Feature 2
- Feature 3
### Code Example
```dart
void main() {
print('Hello, World!');
}
''';
final source = StringMarkdownSource(markdownContent); final converter = MarkdownToPdfConverter(); await converter.convertAndShare(source);
### Custom PDF Options
```dart
final options = const PdfOptions(
title: 'My Custom Document',
author: 'John Doe',
pageFormat: PdfPageFormat.a4,
fontSize: 14.0,
includePageNumbers: true,
headerText: 'Company Document',
footerText: 'Confidential',
);
final converter = MarkdownToPdfConverter(options: options);
HTTP with Custom Headers
final source = HttpMarkdownSource(
url: 'https://api.github.com/repos/user/repo/contents/README.md',
headers: {
'Authorization': 'Bearer your-token',
'Accept': 'application/vnd.github.v3.raw',
},
timeout: const Duration(seconds: 30),
);
final converter = MarkdownToPdfConverter();
await converter.convertAndShare(source);
API Reference
MarkdownToPdfConverter
Main class for converting markdown to PDF.
Methods
convertToFile(MarkdownSource source, {String? fileName})- Convert to PDF fileconvertToBytes(MarkdownSource source)- Convert to PDF bytesconvertAndShare(MarkdownSource source)- Convert and share PDFconvertAndPrint(MarkdownSource source)- Convert and print PDF
MarkdownSource
Abstract class for markdown content sources.
Implementations
HttpMarkdownSource- Fetch markdown from HTTP URLStringMarkdownSource- Use markdown from stringHttpResponseMarkdownSource- Use markdown from HTTP response
PdfOptions
Configuration options for PDF generation.
Properties
pageFormat- PDF page format (default: A4)margins- Page marginstitle- Document titleauthor- Document authorsubject- Document subjectkeywords- Document keywordscreator- Document creatorincludePageNumbers- Include page numbers (default: true)includeTableOfContents- Include table of contents (default: false)headerText- Custom header textfooterText- Custom footer textfontSize- Body text font size (default: 12.0)headingFontSize- Heading font size (default: 16.0)lineHeight- Line height multiplier (default: 1.4)debugMode- Enable debug mode (default: false)
Supported Markdown Features
- β Headers (H1-H6)
- β Bold and italic text
- β Code blocks and inline code
- β Lists (ordered and unordered)
- β Tables
- β Blockquotes
- β Links
- β Horizontal rules
- β Line breaks
Permissions
Android
Add to android/app/src/main/AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
iOS
Add to ios/Runner/Info.plist:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Example App
Check out the example app in the example/ directory to see the package in action.
cd example
flutter run
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
1.0.0
- Initial release
- Support for HTTP and string markdown sources
- Comprehensive PDF formatting
- Customizable PDF options
- Share and print functionality