6

Is there a way I can parse the contents of PDF files into widgets or something more understandable or perhaps into something that can be modified and again converted to PDF format?

Rameshkumar
  • 264
  • 1
  • 12

1 Answers1

0

You can use pdf library to achieve this task

import 'dart:io'; 
import 'package:pdf/pdf.dart' as pw;

Import like this and then use following method to extract text from pdf

String loadPdf({required String pdfPath}) {
  String text = "";
  // Load the PDF file
  var file = File(pdfPath);
  var bytes = file.readAsBytesSync();

  // Decode the PDF file
  var pdf = pw.PdfDocument.fromBytes(bytes);

  // Extract the text from the PDF

  for (var page in pdf.pages) {
    var pageText = page.getText();
    text += pageText;
  }
  return text;
}
Alok Dubey
  • 44
  • 5
  • The `fromBytes()` method/constructor does not seem to exist on the PdfDocument class. Where did you get it from? I'm also trying to read a PDF using just Dart (i.e. not full-fledged Flutter), and most PDF-related packages require `dart:ui` which necessitates Flutter. The `pdf` package seems to be the only one without such constraint, but I cannot figure out how to make it read an existing PDF. Seems it's only intended to generate new PDF files. – Kalex Aug 18 '23 at 21:30