createDocumentFile method
This Create the PDF document and write it to storage path This implementation can throw PathNotFoundException or exceptions based in Storage capabilities
isWeb
is used to know is the current platform is web since the way of the how is saved PDF file
is different from the common on mobile devices or Desktop
Implementation
Future<void> createDocumentFile({
required String path,
void Function(dynamic error)? onException,
void Function([Object? data])? onSucessWrite,
qpdf.DeltaAttributesOptions? deltaOptionalAttr,
bool overrideAttributesPassedByUser = false,
bool shouldProcessDeltas = true,
bool isWeb = false,
}) async {
deltaOptionalAttr ??= qpdf.DeltaAttributesOptions.common();
final qpdf.Converter<Delta, pw.Document> converter = qpdf.PdfService(
pageFormat: pageFormat,
fonts: globalFontsFallbacks,
customBuilders: customBuilders,
onRequestBoldFont: onRequestBoldFont,
onRequestBothFont: onRequestBoldItalicFont,
onRequestFallbacks: onRequestFallbackFont,
onRequestFont: onRequestFont,
onDetectAlignedParagraph: onDetectAlignedParagraph,
onDetectCommonText: onDetectCommonText,
customTheme: themeData,
blockQuoteBackgroundColor: blockQuoteBackgroundColor,
blockQuoteDividerColor: blockQuoteDividerColor,
codeBlockBackgroundColor: codeBlockBackgroundColor,
codeBlockFont: codeBlockFont,
codeBlockNumLinesTextStyle: codeBlockNumLinesTextStyle,
codeBlockTextStyle: codeBlockTextStyle,
blockQuoteTextStyle: blockQuoteTextStyle,
onDetectBlockquote: onDetectBlockquote,
onDetectCodeBlock: onDetectCodeBlock,
onDetectHeaderBlock: onDetectHeaderBlock,
onDetectImageBlock: onDetectImageBlock,
blockQuotePaddingLeft: blockQuotePaddingLeft,
blockQuotePaddingRight: blockQuotePaddingRight,
blockQuotethicknessDividerColor: blockQuotethicknessDividerColor,
onDetectInlineRichTextStyles: onDetectInlineRichTextStyles,
onDetectLink: onDetectLink,
onDetectList: onDetectList,
backM: !shouldProcessDeltas
? backMatterDelta
: processDelta(backMatterDelta, deltaOptionalAttr,
overrideAttributesPassedByUser),
frontM: !shouldProcessDeltas
? frontMatterDelta
: processDelta(frontMatterDelta, deltaOptionalAttr,
overrideAttributesPassedByUser),
onRequestItalicFont: onRequestItalicFont,
document: !shouldProcessDeltas
? document
: processDelta(
document, deltaOptionalAttr, overrideAttributesPassedByUser)!,
);
try {
final pw.Document doc = await converter.generateDoc();
final Uint8List bytes = await doc.save();
if (isWeb) {
List<int> fileInts = List<int>.from(bytes);
web.AnchorElement()
..href =
"data:application/octet-stream;charset=utf-16le;base64,${base64.encode(fileInts)}"
..setAttribute(
"download", "${DateTime.now().millisecondsSinceEpoch}.pdf")
..click();
onSucessWrite?.call('');
return;
}
await File(path).writeAsBytes(bytes);
onSucessWrite?.call(path);
} catch (e) {
onException?.call(e);
}
}