From cfda683ba83a6fe82442035c14af0914bf9de3d5 Mon Sep 17 00:00:00 2001 From: Josh Mulheron Date: Tue, 11 Jun 2024 11:59:39 +0100 Subject: [PATCH] Fix PDF scaling --- src/BinaryKits.Zpl.Viewer/Helpers/UnitsHelper.cs | 10 ++++++++++ src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs | 13 ++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 src/BinaryKits.Zpl.Viewer/Helpers/UnitsHelper.cs diff --git a/src/BinaryKits.Zpl.Viewer/Helpers/UnitsHelper.cs b/src/BinaryKits.Zpl.Viewer/Helpers/UnitsHelper.cs new file mode 100644 index 00000000..6e6f27a1 --- /dev/null +++ b/src/BinaryKits.Zpl.Viewer/Helpers/UnitsHelper.cs @@ -0,0 +1,10 @@ +namespace BinaryKits.Zpl.Viewer.Helpers +{ + static internal class UnitsHelper + { + static internal double ConvertMillimetersToInches(double labelWidth) + { + return labelWidth / 25.4; + } + } +} diff --git a/src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs b/src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs index 70f2c5d8..b86a600e 100644 --- a/src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs +++ b/src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs @@ -1,5 +1,6 @@ using BinaryKits.Zpl.Label.Elements; using BinaryKits.Zpl.Viewer.ElementDrawers; +using BinaryKits.Zpl.Viewer.Helpers; using SkiaSharp; using System; using System.Collections.Generic; @@ -10,6 +11,10 @@ namespace BinaryKits.Zpl.Viewer { public class ZplElementDrawer { + private const int PdfDpi = 72; + private const float ZplDpi = 203.2f; + private const float PdfScaleFactor = PdfDpi / ZplDpi; + private readonly DrawerOptions _drawerOptions; private readonly IPrinterStorage _printerStorage; private readonly IElementDrawer[] _elementDrawers; @@ -110,7 +115,13 @@ public List DrawMulti( // - When drawing PDF we need the Bitmap as well to fix inverted coloring Stream pdfStream = new MemoryStream(); using var document = SKDocument.CreatePdf(pdfStream); - using var pdfCanvas = document.BeginPage(labelImageWidth, labelImageHeight); + + using var pdfCanvas = document.BeginPage( + (float)(UnitsHelper.ConvertMillimetersToInches(labelWidth) * PdfDpi), + (float)(UnitsHelper.ConvertMillimetersToInches(labelHeight) * PdfDpi)); + + pdfCanvas.Scale(PdfScaleFactor, PdfScaleFactor); + if (this._drawerOptions.PdfOutput == true) { skCanvas.AddCanvas(pdfCanvas);