Skip to content

Instantly share code, notes, and snippets.

@gskachkov
Forked from dmslabsbr/geraPdf.dart
Created January 21, 2024 14:02
Show Gist options
  • Save gskachkov/341176d8b83e468cd20edc0efc364ecf to your computer and use it in GitHub Desktop.
Save gskachkov/341176d8b83e468cd20edc0efc364ecf to your computer and use it in GitHub Desktop.
FlutterFlow PDF Action Code
// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/backend/schema/structs/index.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
//import 'dart:js' as js;
import 'package:pdf/pdf.dart';
import 'dart:io';
import 'package:pdf/widgets.dart' as pw;
import 'dart:html' as html;
Future geraPDF() async {
// Add your function code here!
final doc = pw.Document();
doc.addPage(
pw.Page(
build: (pw.Context context) => pw.Center(
child: pw.Text('Hello World!'),
),
),
);
//final file = File('example.pdf');
//await file.writeAsBytes(await pdf.save());
// Gerar o PDF e obter os bytes
List<int> pdfBytes = await doc.save();
print("PDF GERADO");
// Criar um Blob com os bytes do PDF
final blob = html.Blob([Uint8List.fromList(pdfBytes)], 'application/pdf');
// Criar uma URL para o Blob
final url = html.Url.createObjectUrlFromBlob(blob);
// Criar um elemento de âncora (<a>) e configurá-lo para download
final anchor = html.AnchorElement(href: url)
..setAttribute("download", "documento.pdf")
..click();
// Liberar a URL do Blob após o download
html.Url.revokeObjectUrl(url);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment