I am trying to load in images into a PDF that a user takes with their camera. These photos are reporting width as height and height as width.
Attached is the example image used.
console output from below:
flutter: image width (syncfusion): 640
flutter: image height (syncfusion): 480
flutter: image width (flutter) : 480
flutter: image height (flutter) : 640
See example below:
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:syncfusion_flutter_pdf/pdf.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Syncfusion Playground',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
StatecreateState() => _MyHomePageState();
}
class _MyHomePageState extends State{
@override
Widget build(BuildContext context) {
return DefaultTabController(
initialIndex: 1,
length: 2,
child: Scaffold(
appBar: AppBar(
title: const Text('TabBar Sample'),
bottom: const TabBar(
tabs:[
Tab(
icon: Icon(Icons.photo),
),
Tab(
icon: Icon(Icons.picture_as_pdf),
),
],
),
),
body: TabBarView(
children:[
Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: Image.asset("assets/cbimage.png"),
),
),
FutureBuilder(
future: _buildPdf(),
builder: (_, ss) => ss.connectionState == ConnectionState.waiting
? Center(child: CircularProgressIndicator())
: SfPdfViewer.memory(Uint8List.fromList(ss.requireData)),
),
],
),
),
);
}
Future> _buildPdf() async {
var response = await rootBundle.load('assets/cbimage.png');
final directory = await getApplicationDocumentsDirectory();
var file = File("${directory.path}/image.png");
file.writeAsBytesSync(response.buffer.asUint8List());
PdfImage image = PdfBitmap(response.buffer.asUint8List());
print("image width (syncfusion): ${image.width}");
print("image height (syncfusion): ${image.height}");
var decodedImage = await decodeImageFromList(file.readAsBytesSync());
print("image width (flutter) : ${decodedImage.width}");
print("image height (flutter) : ${decodedImage.height}");
var pdf = PdfDocument();
pdf.pages.add();
pdf.pageSettings.size = Size(612, 792);
pdf.pageSettings.margins.all = 0;
pdf.pages[0].graphics.drawImage(
image,
Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble()),
);
return await pdf.save();
}
}
Attachment: cbimage_3bfc69df.zip
We were able to reproduce the reported issue with provided details on our end. Currently, we are validating on this and will update the further details on September 29th, 2023.
We confirmed the issue ”Incorrect image dimensions recorded by PdfBitmap” as a defect in our product and fix will be included in our weekly release, which will be available on October 10, 2023.
Use the below feedback link to track the status of the reported bug.
https://www.syncfusion.com/feedback/47285/incorrect-image-dimensions-recorded-by-pdfbitmap
Disclaimer: “Inclusion of this solution in the weekly release may change due to other factors including but not limited to QA checks and works reprioritization.”
The fix for the reported issue "Incorrect image dimensions recorded by PdfBitmap" has been included in our weekly release v23.1.40.
Kindly use the following link to refer our latest package, https://pub.dev/packages/syncfusion_flutter_pdf/versions