MicrosoftEdge / MicrosoftEdge/WebView2Feedback

Setting printer settings page sizes and margins

Open
#2,389 2 comments 0 reactions 1 assignee View on GitHub

@Lakshmisha-KS is already working on this.

Since Feb 12, 2025.

feature request priority-low tracked
Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

Description

Is your feature request related to a problem? Please describe.
At the moment we must manually find out the correct sizes in inches for paper sizes for these methods:

CHECK_FAILURE(printSettings->put_MarginBottom(0.2));
CHECK_FAILURE(printSettings->put_MarginTop(0.2));
CHECK_FAILURE(printSettings->put_MarginLeft(0.2));
CHECK_FAILURE(printSettings->put_MarginRight(0.2));
CHECK_FAILURE(printSettings->put_PageWidth(8.268)); // A4
CHECK_FAILURE(printSettings->put_PageHeight(11.693)); // A4

Describe the solution you'd like and alternatives you've considered

For the page sizes I would like something like this:

CHECK_FAILURE(printSettings->put_PageSize(PAGE_SIZE_A4, ORIENT_LANDSCAPE));
CHECK_FAILURE(printSettings->put_PageSize(PAGE_SIZE_A4, ORIENT_PORTRAIT));

You could come up with the enum names and the default set of paper sizes. The function would work out under the hood the sizes for the inches itself to pass on to the width / height properties. At the same time we could set the orientation.

Users can continue with the existing API for full custom sizes etc but many of us can default to standard sizes more easily like A4 or LETTER etc.

As for the margins, it would be nice to expose a API similar to:

CHECK_FAILURE(printSettings->put_Margins(0.2));

It would apply it to all sides for you. And perhaps facility for us to set the margin values in mm rather than inches.

The current code:

void CWebBrowser::PrintToPDF(CString strFile)
{
	wil::com_ptr<ICoreWebView2PrintSettings> printSettings = nullptr;

	wil::com_ptr<ICoreWebView2Environment6> webviewEnvironment6;
	CHECK_FAILURE(m_pImpl->m_webViewEnvironment->QueryInterface(
			IID_PPV_ARGS(&webviewEnvironment6)));
	if (webviewEnvironment6)
	{
		CHECK_FAILURE(webviewEnvironment6->CreatePrintSettings(&printSettings));
		CHECK_FAILURE(printSettings->put_ShouldPrintBackgrounds(TRUE));
		CHECK_FAILURE(printSettings->put_ShouldPrintHeaderAndFooter(FALSE));
		CHECK_FAILURE(printSettings->put_MarginBottom(0.2));
		CHECK_FAILURE(printSettings->put_MarginTop(0.2));
		CHECK_FAILURE(printSettings->put_MarginLeft(0.2));
		CHECK_FAILURE(printSettings->put_MarginRight(0.2));
		CHECK_FAILURE(printSettings->put_PageWidth(8.268)); // A4
		CHECK_FAILURE(printSettings->put_PageHeight(11.693)); // A4

	}

	wil::com_ptr<ICoreWebView2_7> webview2_7;
	CHECK_FAILURE(m_pImpl->m_webView->QueryInterface(IID_PPV_ARGS(&webview2_7)));
	if (webview2_7)
	{
		//m_printToPdfInProgress = true;
		CHECK_FAILURE(webview2_7->PrintToPdf(
			strFile, printSettings.get(),
			Callback<ICoreWebView2PrintToPdfCompletedHandler>(
				[this](HRESULT errorCode, BOOL isSuccessful) -> HRESULT {
					CHECK_FAILURE(errorCode);
					//m_printToPdfInProgress = false;
					AsyncMessageBox(
						(isSuccessful) ? L"Print to PDF succeeded"
						: L"Print to PDF failed",
						L"Print to PDF Completed");
					return S_OK;
				})
			.Get()));
	}
}

AB#39415159

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.