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

问题描述 投票:0回答:3

我正在尝试将使用 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');
flutter dart pdf docx syncfusion
3个回答
1
投票

使用此包或任何支持字节输入的包

https://pub.dev/packages/syncfusion_flutter_pdf

您可以将文档转换为字节并生成 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();


1
投票

Kaushik Chandru 的答案对我不起作用: [错误:flutter/runtime/dart_vm_initializer.cc(41)] 未处理的异常:RangeError(结束):无效值:不在包含范围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(包:syncfusion_flutter_pdf/src/pdf/implementation/io/cross_table.dart:503:38)
#3 CrossTable._initialize(包:syncfusion_flutter_pdf/src/pdf/implementation/io/cross_table.dart:112:32)
#4 新的 CrossTable(包:syncfusion_flutter_pdf/src/pdf/implementation/io/cross_table.dart:27:5) #5 PdfCrossTable._initializeCrossTable(包:syncfusion_flutter_pdf/src/pdf/implementation/io/pdf_cross_table.dart:177:19) #6 新的 PdfCrossTable (包:syncfusion_flutter_pdf/src/pdf/implementation/io/pdf_cross_table.dart:41:9) #7 PdfDocument._initialize(包:syncfusion_flutter_pdf/src/pdf/implementation/pdf_document/pdf_document.dart:757:28) #8 新的 PdfDocument (包:syncfusion_flutter_pdf/src/pdf/implementation/pdf_document/pdf_document.dart:78:5)
#9 loadDocument(包:summary_ai/load_document.dart:19:7)


0
投票

我也有同样的问题。有什么解决办法吗?

© www.soinside.com 2019 - 2024. All rights reserved.