Skip to content

Instantly share code, notes, and snippets.

@dmslabsbr
Created November 23, 2023 20:08
Show Gist options
  • Save dmslabsbr/87791d5a8719faca95bc09ce52be2498 to your computer and use it in GitHub Desktop.
Save dmslabsbr/87791d5a8719faca95bc09ce52be2498 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);
}
@dmslabsbr
Copy link
Author

Pubspec Dependencies:

pdf: ^3.10.6

Exemplo v1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment