4Q-s-r-o / signature

Flutter plugin that creates a canvas for writing down a signature
MIT License
255 stars 85 forks source link

Export Signature to PDF #38

Closed mame306 closed 3 years ago

mame306 commented 3 years ago

Hi,

I want to export the signature to a pdf file. Is it possible to do that? And how could I implement it?

Thanks for helping me

bdlukaa commented 3 years ago

You can do this just by saving a file with the extension .pdf.

See this stackoverflow answer

MartinHlavna commented 3 years ago

Hi,

  1. SignatureController allows you to export signature as pdf image by calling toPngBytes()
  2. If you want to save signature to pdf file you have to create it. pdf package is great way to implement it.

Quick idea (i have not tested it, there may be errors, but it should point you to right direction):

import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
//...

var pngBytes = signatureController.toPngBytes();
final pdf = pw.Document();
final image = pw.MemoryImage(
  pngBytes,
);

pdf.addPage(pw.Page(build: (pw.Context context) {
  return pw.Center(
    child: pw.Image(image),
  ); // Center
})); // Page

// On Flutter, use the [path_provider](https://pub.dev/packages/path_provider) library:
final output = await getTemporaryDirectory();
final file = File("${output.path}/example.pdf");
await file.writeAsBytes(await pdf.save());