Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
WANGNEWMAN opened this issue Oct 15, 2024 · 3 comments
Open

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

WANGNEWMAN opened this issue Oct 15, 2024 · 3 comments
Labels
needs triage support Asking for help writing an application

Comments

@WANGNEWMAN
Copy link

WANGNEWMAN commented Oct 15, 2024

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

@WANGNEWMAN WANGNEWMAN added needs triage support Asking for help writing an application labels Oct 15, 2024
@brunoccosta
Copy link

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
Copy link
Owner

DavBfr commented Oct 22, 2024

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
Copy link
Author

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage support Asking for help writing an application
Projects
None yet
Development

No branches or pull requests

4 participants
@DavBfr @brunoccosta @WANGNEWMAN and others