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

Page Format Issue #1800

Open
1 task
DevMensIT opened this issue Dec 26, 2024 · 8 comments
Open
1 task

Page Format Issue #1800

DevMensIT opened this issue Dec 26, 2024 · 8 comments
Labels
bug Something isn't working needs triage

Comments

@DevMensIT
Copy link

Describe the bug
Problem is when passing page format to a5 or a6 to direct printer it sets to a4 page to print.
when try to direct print it take a4 format only not other format.
After export and open pdf then try to print it automatically set page format which we pass inside 'format'

To Reproduce
Code snippet to reproduce the behavior:

await Printing.layoutPdf(
                dynamicLayout: false,
                format: PdfPageFormat.a5,
                usePrinterSettings: false,
                forceCustomPrintPaper: true,
                onLayout: (format) => _generatePdf(format, 'Xyz'));

Expected behavior
image

Screenshots
image

Flutter Doctor
image

Desktop (please complete the following information):

  • Windows

Smartphone (please complete the following information):

  • Device: Laptop
  • OS: Windows 11
  • Desktop Application
@DevMensIT DevMensIT added bug Something isn't working needs triage labels Dec 26, 2024
@DavBfr
Copy link
Owner

DavBfr commented Dec 26, 2024

Maybe Windows can't detect the paper size from the width and length with dmPaperSize = 0.
This value needs to be computed: https://github.com/DavBfr/dart_pdf/blob/master/printing/windows/print_job.cpp#L102C9-L102C20 from the PDF Width and Height with a matching table. And if the value is not found, use 0 with the dmPaperWidth and dmPaperLength

See: https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-devmodew

@DevMensIT
Copy link
Author

Maybe Windows can't detect the paper size from the width and length with dmPaperSize = 0. This value needs to be computed: https://github.com/DavBfr/dart_pdf/blob/master/printing/windows/print_job.cpp#L102C9-L102C20 from the PDF Width and Height with a matching table. And if the value is not found, use 0 with the dmPaperWidth and dmPaperLength

See: https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-devmodew

But after exporting same data to pdf and then print it worked.

can you tell me what should i do to overwrite default page format ?

@DavBfr
Copy link
Owner

DavBfr commented Dec 26, 2024

I guess write a list of if statements to check the width and height to define the paper size.
like: if ((int) width == 419 && (int)height == 595) { dm->dmPaperSize = DMPAPER_A5; } else if (...) ... for all predefined formats in the pdf library, portrait and landscape.

Modes are defined here: https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-devmodea
Formats here: https://github.com/DavBfr/dart_pdf/blob/master/pdf/lib/src/pdf/page_format.dart

@DevMensIT
Copy link
Author

I guess write a list of if statements to check the width and height to define the paper size. like: if ((int) width == 419 && (int)height == 595) { dm->dmPaperSize = DMPAPER_A5; } else if (...) ... for all predefined formats in the pdf library, portrait and landscape.

Modes are defined here: https://learn.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-devmodea Formats here: https://github.com/DavBfr/dart_pdf/blob/master/pdf/lib/src/pdf/page_format.dart

Actually i'm using pre-defined format only and i don't have any idea where should i write those conditions. i'm clueless.

@DavBfr
Copy link
Owner

DavBfr commented Dec 26, 2024

In the cpp code for Windows.

@DevMensIT
Copy link
Author

In the cpp code for Windows.

okay will checkout. thanks for your time.
if you have time then you can please checkout too.

@DevMensIT
Copy link
Author

Hey below code works well if you can put in next update it will big help to those who facing that problem :)

void setPaperSize(DEVMODE *dm, double width, double height)
  {
    int intWidth = static_cast<int>(width);   // Convert width to integer
    int intHeight = static_cast<int>(height); // Convert height to integer
    std::cout << "Width: " << width << ", Height: " << height << std::endl;

    if ((intWidth == 595 && intHeight == 842) || (intWidth == 842 && intHeight == 595))
    {
      std::cout << "A4" << std::endl;
      dm->dmPaperSize = DMPAPER_A4; // A4 size
    }
    else if ((intWidth == 419 && intHeight == 595) || (intWidth == 595 && intHeight == 419))
    {
      std::cout << "A5" << std::endl;
      dm->dmPaperSize = DMPAPER_A5; // A5 size
    }
    else if ((intWidth == 612 && intHeight == 792) || (intWidth == 792 && intHeight == 612))
    {
      std::cout << "LETTER" << std::endl;
      dm->dmPaperSize = DMPAPER_LETTER; // Letter size
    }
    else if ((intWidth == 297 && intHeight == 421) || (intWidth == 421 && intHeight == 297))
    {
      std::cout << "A6" << std::endl;
      dm->dmPaperSize = DMPAPER_A6; // A6 size
    }
    else if ((intWidth == 612 && intHeight == 1008) || (intWidth == 1008 && intHeight == 612))
    {
      std::cout << "Legal" << std::endl;
      std::cout << "Inside Legal: " << std::endl;
      dm->dmPaperSize = DMPAPER_LEGAL; // Legal size
    }
    else if ((intWidth == 842 && intHeight == 1191) || (intWidth == 1191 && intHeight == 842))
    {
      std::cout << "A3" << std::endl;
      dm->dmPaperSize = DMPAPER_A3; // Tabloid size
    }
    else
    {
      std::cout << "No Page Found" << std::endl;
      dm->dmPaperSize = 0;                                                  // Custom paper size
      dm->dmPaperWidth = static_cast<short>(round(width * 254 / pdfDpi));   // Convert width to DEVMODE units
      dm->dmPaperLength = static_cast<short>(round(height * 254 / pdfDpi)); // Convert height to DEVMODE units
    }
    // Explicitly set the orientation based on paper size
    if (width > height)
    {
      dm->dmOrientation = DMORIENT_LANDSCAPE; // Landscape
    }
    else
    {
      dm->dmOrientation = DMORIENT_PORTRAIT; // Portrait
    }

    // Update dmFields to reflect changes
    dm->dmFields |= DM_ORIENTATION | DM_PAPERSIZE | DM_PAPERLENGTH | DM_PAPERWIDTH;
  }

Above code is i put in cpp file and below code you can see i called that function

bool PrintJob::printPdf(const std::string &name,
                          std::string printer,
                          double width,
                          double height,
                          bool usePrinterSettings)
  {
    documentName = name;

    std::size_t dmSize = sizeof(DEVMODE);
    std::size_t dmExtra = 0;

    if (!printer.empty())
    {
      dmExtra = DeviceCapabilities(fromUtf8(printer).c_str(), NULL, DC_EXTRA,
                                   NULL, NULL);
    }

    auto dm = static_cast<DEVMODE *>(GlobalAlloc(0, dmSize + dmExtra));

    if (usePrinterSettings)
    {

      dm = nullptr; // to use default driver config
    }
    else
    {

      ZeroMemory(dm, sizeof(DEVMODE));
      dm->dmSize = (WORD)dmSize;
      dm->dmDriverExtra = (WORD)dmExtra;
      dm->dmFields =
          DM_ORIENTATION | DM_PAPERSIZE | DM_PAPERLENGTH | DM_PAPERWIDTH;
      // dm->dmPaperSize = 0;
      // Dynamically set paper size
      std::cout << "Width: " << width << ", Height: " << height << ", Printer Setting : " << usePrinterSettings << std::endl;

      setPaperSize(dm, width, height);
    }

  }

@DevMensIT DevMensIT reopened this Dec 27, 2024
@DevMensIT
Copy link
Author

One More Problem i found is margin is getting overwrite by default margin in cpp

 auto marginLeft =
        static_cast<double>(GetDeviceCaps(hDC, PHYSICALOFFSETX)) / dpiX;
    auto marginTop =
        static_cast<double>(GetDeviceCaps(hDC, PHYSICALOFFSETY)) / dpiY;
    auto marginRight = pageWidth - printableWidth - marginLeft;
    auto marginBottom = pageHeight - printableHeight - marginTop;

i'm passing the margins

 // PdfPageFormat pageFormat = PdfPageFormat.a5;
  final PdfPageFormat pageFormat = const PdfPageFormat(
    5.83 * PdfPageFormat.inch,
    8.27 * PdfPageFormat.inch,
    marginLeft: 5, // Minimal left margin
    marginTop: 5, // Minimal top margin
    marginRight: 5, // Minimal right margin
    marginBottom: 5, // Minimal bottom margin
  );

how to handle this ?

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

No branches or pull requests

2 participants