如何在flutter中转换docx于pdf文件?

oknwwptz  于 2023-04-07  发布在  Flutter
关注(0)|答案(2)|浏览(344)

我正在尝试转换我用docx_template库创建的docx文件:^0.3.3到PDF,我已经尝试过使用库来做到这一点。Whit SyncFusion我有这个:

//Get external storage directory
    final directory = await getApplicationDocumentsDirectory();
    //Get directory path
    String path = directory.path;

    final of = File('$path/Engranes Rectos Sistema Ingles.docx');
    if (d != null) await of.writeAsBytes(d);

    Directory directory2 = (await getApplicationDocumentsDirectory());
    String path2 = directory2.path;

    final PdfDocument document = PdfDocument(
        inputBytes: File('$path/Engranes Rectos Sistema Ingles.docx')
            .readAsBytesSync());

    //Save the document.
    File file = File('$path/Engranes Rectos Sistema Ingles.pdf');
    await file.writeAsBytes(await document.save(), flush: true);
    //Dispose the document.
    document.dispose();
    OpenFile.open('$path/Engranes Rectos Sistema Ingles.pdf');
fjaof16o

fjaof16o1#

使用此包或任何支持字节输入的包
https://pub.dev/packages/syncfusion_flutter_pdf
您可以像下面这样将doc转换为bytes并生成pdf

//Load the existing docx document.
final PdfDocument document =
    PdfDocument(inputBytes: File('input.docx').readAsBytesSync());

//Save the document.
File('yourFileName.pdf').writeAsBytes(await document.save());
//Dispose the document.
document.dispose();
wlp8pajw

wlp8pajw2#

Kaushik Chandru的回答对我不起作用:[错误:flutter/runtime/dart_vm_initializer. cc(41)]无法处理的异常:RangeError(end):无效值:不在包含范围2048..12210内:1024 #0 RangeError. checkValidRange(dart:core/errors. dart:379:9)#1_TypedIntListMixin. sublist(dart:typed_data-patch/typed_data_patch. dart:466:31)#2 CrossTable._checkJunk(package:syncfusion_flutter_pdf/src/pdf/implementation/io/cross_table. dart:503:38)
#3 CrossTable._initialize(package:syncfusion_flutter_pdf/src/pdf/implementation/io/cross_table. dart:112:32)
#4 new CrossTable(package:syncfusion_flutter_pdf/src/pdf/implementation/io/cross_table. dart:27:5)#5 PdfCrossTable._initializeCrossTable(package:syncfusion_flutter_pdf/src/pdf/implementation/io/pdf_cross_table. dart:177:19)#6 new PdfCrossTable(package:syncfusion_flutter_pdf/src/pdf/implementation/io/pdf_cross_table. dart:41:9)#7 PdfDocument._initialize(package:syncfusion_flutter_pdf/src/pdf/implementation/pdf_document/pdf_document. dart:757:28)#8 new PdfDocument(package:syncfusion_flutter_pdf/src/pdf/implementation/pdf_document/pdf_document. dart:78:
#9 loadDocument(package:summary_ai/load_document. dart:19:7)

相关问题