DavBfr / dart_pdf

Pdf creation module for dart/flutter
https://pub.dev/packages/pdf
Apache License 2.0
1.42k stars 632 forks source link

editPage result some PDF is mirror, most of result is normal #1764

Open WANGNEWMAN opened 1 month ago

WANGNEWMAN commented 1 month ago

GestureDetector collect flutter's Path Object draw in CustomPaint

class Stroke {
  final path = Path();
  final time = DateTime.now().microsecondsSinceEpoch;
  final Color color;
  final double width;

  Stroke({
    this.color = Colors.black,
    this.width = 4,
  });
}

class BoardPainter extends CustomPainter {
  BoardPainter({
    this.strokes,
  });

  final List<Stroke>? strokes;

   @override
  void paint(Canvas canvas, Size size) {
    canvas.clipRect(Rect.fromLTWH(0, 0, size.width, size.height));

    canvas.drawRect(
      Rect.fromLTWH(0, 0, size.width, size.height),
      Paint()..color = Colors.transparent, //Colors.white,
    );
    canvas.saveLayer(Rect.fromLTWH(0, 0, size.width, size.height), Paint());
    for (final stroke in strokes!) {
      final paint = Paint()
        ..strokeWidth = stroke.width
        ..color = stroke.color
        ..strokeCap = StrokeCap.round
        ..style = PaintingStyle.stroke
        ..isAntiAlias = true
        ..blendMode = stroke.color == Colors.transparent ? BlendMode.clear : BlendMode.srcOver;
      canvas.drawPath(stroke.path, paint);
    }
    canvas.restore();
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

after Drawing use UI.PictureRecorder to generate png

Future<UI.Image>? get uiImage {
    UI.PictureRecorder recorder = UI.PictureRecorder();
    Canvas canvas = Canvas(recorder);
    BoardPainter painter = BoardPainter(strokes: strokes);
    painter.paint(canvas, _size);
    return recorder
        .endRecording()
        .toImage(_size.width.floor(), _size.height.floor());

  }

At last, composite png to PDF

ByteData? byteData =
          await image.toByteData(format: UI.ImageByteFormat.png);
Uint8List picBytes = byteData!.buffer.asUint8List();
PdfImage img = PdfImage.file(pdf.document,
  bytes: picBytes,
  orientation:PdfImageOrientation.topLeft);

pdf.editPage(controller.index, pw.Page(pageFormat: page.pageFormat, margin: pw.EdgeInsets.zero, build:(context) {
    final flag = page.pageFormat.width/page.pageFormat.height;
    final matrix = Matrix4.identity();
    return pw.FullPage(ignoreMargins: true, child: pw.Transform(transform: matrix, child: pw.Align(child: pw.Image(pw.ImageProxy(img), width:page.pageFormat.width, height: page.pageFormat.height))));
},));

normal png wecom-temp-1043938-86090e1d997a1ed444d243fd563ae60d wecom-temp-1013250-a0387855169ed365e8a8f5ab0289649b

mirror png(And the composite page page does not occupy the entire page) wecom-temp-1416340-f2c552050bf613b2ac533b6e9a63b1f8 wecom-temp-1418401-20ff7f4dad1b63441b3f61c5dcf5adf8

brunoccosta commented 1 month ago

Im facing the same issue here. also, some PDFs get the content of the edited page replaced. I mean, the page gets all blank with the image in the center instead of overlap

DavBfr commented 1 month ago

For the issues with the page edits upside down, it's usually because the original page is not saving + restoring the state. In this case, you can use the protectContents: true of the PdfDocumentParser object.

WANGNEWMAN commented 3 weeks ago

thanks