html_to_image 1.0.0+1 copy "html_to_image: ^1.0.0+1" to clipboard
html_to_image: ^1.0.0+1 copied to clipboard

Flutter plugin to convert HTML file to image on Android and iOS using WebView on Android and WkWebView on iOS.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:html_to_image/html_to_image.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late final TextEditingController _controller;
  Uint8List? img;

  static const _dummyContent = '''
  <html>
  <head>
  <title>
  Example of Paragraph tag
  </title>
  </head>
  <body>
  <p> <!-- It is a Paragraph tag for creating the paragraph -->
  <b> HTML </b> stands for <i> <u> Hyper Text Markup Language. </u> </i> It is used to create a web pages and applications. This language
  is easily understandable by the user and also be modifiable. It is actually a Markup language, hence it provides a flexible way for designing the
  web pages along with the text.
  <img src="https://picsum.photos/200/300" />
  <br />
  </body>
  </html>
  ''';

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController(text: _dummyContent);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  Future<void> convertToImage() async {
    final image = await HtmlToImage.tryConvertToImage(
      content: _controller.text,
    );
    setState(() {
      img = image;
    });
  }

  Future<void> convertToImageFromAsset() async {
    final image = await HtmlToImage.convertToImageFromAsset(
      asset: 'assets/example.html',
    );
    setState(() {
      img = image;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('HTML to Image Converter'),
        ),
        body: img == null
            ? Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  children: [
                    Expanded(
                      child: TextField(
                        controller: _controller,
                        maxLines: 100,
                      ),
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        ElevatedButton(
                          onPressed: convertToImage,
                          child: const Text('Convert to Image'),
                        ),
                        ElevatedButton(
                          onPressed: convertToImageFromAsset,
                          child: const Text('Convert from Asset'),
                        ),
                      ],
                    ),
                  ],
                ),
              )
            : Image.memory(img!),
        floatingActionButton: img != null
            ? FloatingActionButton(
                onPressed: () {
                  setState(() {
                    img = null;
                  });
                },
                tooltip: 'Clear',
                child: const Icon(Icons.clear),
              )
            : null,
      ),
    );
  }
}
3
likes
160
points
204
downloads
screenshot

Publisher

verified publisherkedark.com.np

Weekly Downloads

Flutter plugin to convert HTML file to image on Android and iOS using WebView on Android and WkWebView on iOS.

Homepage
Repository (GitHub)
View/report issues

Topics

#html #html-to-image #html-print

Documentation

Documentation
API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on html_to_image

Packages that implement html_to_image