Skip to content

Commit

Permalink
Fix pdf export
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Jan 31, 2024
1 parent f3a88ce commit 9cfe6cc
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 109 deletions.
230 changes: 121 additions & 109 deletions app/lib/dialogs/export/pdf.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:async';

import 'package:butterfly/api/save.dart';
import 'package:butterfly_api/butterfly_api.dart';
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -46,118 +49,127 @@ class _PdfExportDialogState extends State<PdfExportDialog> {
return const Center(child: CircularProgressIndicator());
}
final currentIndex = state.currentIndexCubit;
return Column(mainAxisSize: MainAxisSize.min, children: [
Header(
title: Text(AppLocalizations.of(context).exportPdf),
actions: [
IconButton(
icon: const PhosphorIcon(PhosphorIconsLight.list),
tooltip: AppLocalizations.of(context).presets,
onPressed: () async {
final preset = await showDialog<ExportPreset>(
context: context,
builder: (ctx) => BlocProvider.value(
value: context.read<DocumentBloc>(),
child: ExportPresetsDialog(areas: areas)));
if (preset != null) {
setState(() {
areas.clear();
areas.addAll(preset.areas);
});
}
},
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Header(
title: Text(AppLocalizations.of(context).exportPdf),
actions: [
IconButton(
icon: const PhosphorIcon(PhosphorIconsLight.list),
tooltip: AppLocalizations.of(context).presets,
onPressed: () async {
final preset = await showDialog<ExportPreset>(
context: context,
builder: (ctx) => BlocProvider.value(
value: context.read<DocumentBloc>(),
child: ExportPresetsDialog(areas: areas)));
if (preset != null) {
setState(() {
areas.clear();
areas.addAll(preset.areas);
});
}
},
),
IconButton(
onPressed: () async {
final result = await showDialog<(String, String)>(
context: context,
builder: (context) =>
_AreaSelectionDialog(document: state.data),
);
if (result != null) {
final (page, area) = result;
setState(() {
areas.add(AreaPreset(name: area, page: page));
});
}
},
icon: const PhosphorIcon(PhosphorIconsLight.plus),
tooltip: AppLocalizations.of(context).add,
)
],
),
IconButton(
onPressed: () async {
final result = await showDialog<(String, String)>(
context: context,
builder: (context) =>
_AreaSelectionDialog(document: state.data),
);
if (result != null) {
final (page, area) = result;
setState(() {
areas.add(AreaPreset(name: area, page: page));
});
}
},
icon: const PhosphorIcon(PhosphorIconsLight.plus),
tooltip: AppLocalizations.of(context).add,
)
],
),
Flexible(
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: 20, vertical: 15),
child: Column(mainAxisSize: MainAxisSize.min, children: [
Flexible(
child: SingleChildScrollView(
child: Wrap(
alignment: WrapAlignment.center,
crossAxisAlignment: WrapCrossAlignment.center,
children: areas.mapIndexed((i, e) {
final area =
e.area ?? state.page.getAreaByName(e.name);
if (area == null) {
return Container();
}
return FutureBuilder<ByteData?>(
future: currentIndex.render(
state.data, state.page, state.info,
width: area.width,
height: area.height,
quality: e.quality,
x: area.position.x,
y: area.position.y),
builder: (context, snapshot) => _AreaPreview(
area: area,
quality: e.quality,
onRemove: () {
setState(() {
areas.removeAt(i);
});
},
onQualityChanged: (value) {
setState(() {
areas[i] = e.copyWith(quality: value);
});
},
image: snapshot.data?.buffer.asUint8List(),
),
);
}).toList(),
Flexible(
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20, vertical: 15),
child: Column(mainAxisSize: MainAxisSize.min, children: [
Flexible(
child: SingleChildScrollView(
child: Wrap(
alignment: WrapAlignment.center,
crossAxisAlignment: WrapCrossAlignment.center,
children: areas.mapIndexed((i, e) {
final area =
e.area ?? state.page.getAreaByName(e.name);
if (area == null) {
return Container();
}
return FutureBuilder<ByteData?>(
future: currentIndex.render(
state.data, state.page, state.info,
width: area.width,
height: area.height,
quality: e.quality,
x: area.position.x,
y: area.position.y),
builder: (context, snapshot) => _AreaPreview(
area: area,
quality: e.quality,
onRemove: () {
setState(() {
areas.removeAt(i);
});
},
onQualityChanged: (value) {
setState(() {
areas[i] = e.copyWith(quality: value);
});
},
image: snapshot.data?.buffer.asUint8List(),
),
);
}).toList(),
),
),
),
),
const Divider(),
Row(
children: [
Expanded(child: Container()),
TextButton(
child: Text(AppLocalizations.of(context).cancel),
onPressed: () => Navigator.of(context).pop(),
),
ElevatedButton(
child: Text(widget.print
? AppLocalizations.of(context).print
: AppLocalizations.of(context).export),
onPressed: () async {
Future<Uint8List> getBytes() async =>
(await currentIndex.renderPDF(
state.data, state.info,
areas: areas))
.save();
Navigator.of(context).pop();
if (widget.print) {
Printing.layoutPdf(
onLayout: (_) => getBytes(),
);
return;
}
exportPdf(context, await getBytes());
},
),
],
)
]),
),
const Divider(),
Row(
children: [
Expanded(child: Container()),
TextButton(
child: Text(AppLocalizations.of(context).cancel),
onPressed: () => Navigator.of(context).pop(),
),
ElevatedButton(
child: Text(widget.print
? AppLocalizations.of(context).print
: AppLocalizations.of(context).export),
onPressed: () async {
Navigator.of(context).pop();
Printing.layoutPdf(
onLayout: (_) async => (await currentIndex
.renderPDF(state.data, state.info,
areas: areas))
.save(),
);
},
),
],
)
]),
),
),
]);
),
]);
}),
),
);
Expand Down
2 changes: 2 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/91.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Fix export pdf print instead of export as file
* Fix scroll in pdf export dialog

0 comments on commit 9cfe6cc

Please sign in to comment.