How can I parse the contents of a PDF file in dart?

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?

Upvotes: 6

Views: 1886

Answers (2)

Rohinton Collins
Rohinton Collins

Reputation: 76

Adding to @alok-dubey's answer. The pdf package has a paid tier for $250 which includes:

  • Document encryption1 with AES 256 and other algorithms.
  • Existing PDF modification, for adding pages, and updating existing pages.
  • Digital signature, including signing existing documents.
  • Existing PDF digital signature validation.

This is very hard to find, but if you scroll to the bottom of the pub.dev page you will see a link to this page with more information: https://pub.nfet.net/pdf_crypto/

But looking at the OPs question, this is what they want.

The same publisher (nfet.net) also wrote the printing package which can be used to preview PDFs using the PdfPreview class.

Upvotes: 0

Alok Dubey
Alok Dubey

Reputation: 901

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;
}

Upvotes: -2

Related Questions