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

Unable to remove the default margin when using Printing.convertHtml(..) function. #135

Closed
jazzbpn opened this issue Aug 26, 2019 · 10 comments
Labels
bug Something isn't working

Comments

@jazzbpn
Copy link

jazzbpn commented Aug 26, 2019

How to remove the outer margin shown in the android and iOS attachment below?

@dav Is there any solution to this issue. I am waiting for your reply. Thank you for your time. Asked at stack overflow

  Future<void> printPdfMethod() async {
    print('Print ...');
    await Printing.layoutPdf(onLayout: (PdfPageFormat format) async {
      return await Printing.convertHtml(
          format: PdfPageFormat.a4
              .applyMargin(left: 0, top: 0, right: 0, bottom: 0),
          html:
              '<?xml version="1.0"?><html><body style="margin:0;padding:0" bgcolor="#144434"><h1 style="color:white">Test Layout Margin</h1></body></html>');
    });
  }

Android Screenshot

Screenshot_20190826_174829_com android printspooler

iOS Screenshot

Untitled

@DavBfr
Copy link
Owner

DavBfr commented Sep 2, 2019

Try to reset the margins on the css, like html, body { margin: 0; padding:0}

@jazzbpn
Copy link
Author

jazzbpn commented Sep 3, 2019

This works on android but not in iOS. Updated question at stack overflow

We use this, there is a right-margin issue and other left-top-bottom-margin is ok in iOS app.

format: PdfPageFormat.a4
              .applyMargin(left: 0, top: 0, right: 0, bottom: 0),

@DavBfr
Copy link
Owner

DavBfr commented Sep 6, 2019

If you want to set the margin regardless, use copyWith(). applyMargin will only increase the margin from what you already have. And you also have to use the provided paper format:

await Printing.layoutPdf(onLayout: (PdfPageFormat format) async {
      return await Printing.convertHtml(
          format: format.copyWith(marginLeft: 0, marginTop: 0, marginRight: 0, marginBottom: 0),
          html: '<?xml version="1.0"?><html><body style="margin:0;padding:0" bgcolor="#144434"><h1 style="color:white">Test Layout Margin</h1></body></html>');
    });

@DavBfr DavBfr closed this as completed Sep 6, 2019
@jazzbpn
Copy link
Author

jazzbpn commented Sep 6, 2019

Hello @DavBfr , Please do not close this issue. Thank you so much for the answer. This works on printing case but there is still a problem when we want to save this PDF file and view that created PDF like: (Result shown in the attachment.)

  Future<void> saveAsFileHTML(BuildContext context) async {
    final Directory appDocDir = await getApplicationDocumentsDirectory();
    final String appDocPath = appDocDir.path;
    final File file = File(appDocPath + '/' + 'document.pdf');
    print('Save as file ${file.path} ...');

    await file.writeAsBytes((await Printing.convertHtml(
      format: PdfPageFormat.a4.copyWith(
          height: PdfPageFormat.a4.height,
          width: PdfPageFormat.a4.width,
          marginBottom: 0,
          marginLeft: 0,
          marginRight: 0,
          marginTop: 0),
      html: '<?xml version="1.0"?><html><body style="margin:0;padding:0" bgcolor="#144434"><h1 style="color:white">Test Layout Margin</h1></body></html>',
    )));

    Navigator.push<dynamic>(
      context,
      MaterialPageRoute<dynamic>(
          builder: (BuildContext context) => PdfViewer(file: file)),
    );
  }

now

@DavBfr DavBfr reopened this Sep 8, 2019
@jazzbpn
Copy link
Author

jazzbpn commented Sep 9, 2019

@DavBfr I'm waiting for your reply. Is there any ways to resolve this issue? Thank you for your time.

@jazzbpn
Copy link
Author

jazzbpn commented Sep 12, 2019

@DavBfr How can we resolve this margin issue when previewing the PDF?

now

  Future<void> saveAsFileHTML(BuildContext context) async {
    final Directory appDocDir = await getApplicationDocumentsDirectory();
    final String appDocPath = appDocDir.path;
    final File file = File(appDocPath + '/' + 'document.pdf');
    print('Save as file ${file.path} ...');

    await file.writeAsBytes((await Printing.convertHtml(
      format: PdfPageFormat.a4.copyWith(
          height: PdfPageFormat.a4.height,
          width: PdfPageFormat.a4.width,
          marginBottom: 0,
          marginLeft: 0,
          marginRight: 0,
          marginTop: 0),
      html: '<?xml version="1.0"?><html><body style="margin:0;padding:0" bgcolor="#144434"><h1 style="color:white">Test Layout Margin</h1></body></html>',
    )));

    Navigator.push<dynamic>(
      context,
      MaterialPageRoute<dynamic>(
          builder: (BuildContext context) => PdfViewer(file: file)),
    );
  }

@DavBfr
Copy link
Owner

DavBfr commented Sep 12, 2019

This is fixed in version 2.1.4, don't hesitate to Buy Me A Coffee

@DavBfr DavBfr added the bug Something isn't working label Sep 12, 2019
@DavBfr DavBfr closed this as completed Sep 12, 2019
@jazzbpn
Copy link
Author

jazzbpn commented Sep 12, 2019 via email

@DavBfr
Copy link
Owner

DavBfr commented Sep 12, 2019

You can use my bitcoin address: 1QGdK5kuoUBLJDFgtTFCg9WwKDG8Qt4BA2

@jazzbpn
Copy link
Author

jazzbpn commented Sep 13, 2019

But I do not have any bitcoin. I was applied for Payoneer debit card. As soon as I received the card, I will buy you a coffee via PayPal. Thankyou!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants