pdf_handler 0.0.1
pdf_handler: ^0.0.1 copied to clipboard
Compress PDF
example/lib/main.dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:pdf_handler/pdf_handler.dart';
import 'package:pdf_handler/pdf_preview.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'PDF Handler Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
PDFHandler pdfHandler = PDFHandler();
bool isProcessing = false;
File? pdfFile;
File? compressedPdfFile;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('PDF handler'),
),
body: Container(
padding: const EdgeInsets.all(24),
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
if (pdfFile != null) ...[
Text('Original File path - \n${pdfFile?.path}'),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () async {
try {
isProcessing = true;
setState(() {});
compressedPdfFile =
await pdfHandler.compressPdf(pdfFile!, 100 * 1024);
isProcessing = false;
setState(() {});
} catch (exp) {
debugPrint('exp - $exp');
}
},
child: isProcessing
? const SizedBox(
height: 16,
width: 16,
child: CircularProgressIndicator(strokeWidth: 2))
: const Text('Compress PDF'),
),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PdfPreviewScreen(
pdfFilePath: pdfFile!.path,
),
),
);
},
child: const Text('View original PDF'),
)
],
if (compressedPdfFile != null) ...[
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PdfPreviewScreen(
pdfFilePath: compressedPdfFile!.path,
),
),
);
},
child: const Text('View compressed PDF'),
)
],
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
pdfFile = await pdfHandler.selectPdf();
setState(() {});
},
tooltip: 'Upload Pdf',
child: const Icon(Icons.upload_file),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}