Skip to content

Commit 7975253

Browse files
committed
Fix PDF scaling
1 parent 4a039ce commit 7975253

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace BinaryKits.Zpl.Viewer.Helpers
4+
{
5+
static internal class UnitsHelper
6+
{
7+
static internal int ConvertMillimetersToNearestInch(double labelWidth)
8+
{
9+
return Convert.ToInt32(labelWidth / 25.4);
10+
}
11+
}
12+
}

src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BinaryKits.Zpl.Label.Elements;
22
using BinaryKits.Zpl.Viewer.ElementDrawers;
3+
using BinaryKits.Zpl.Viewer.Helpers;
34
using SkiaSharp;
45
using System;
56
using System.Collections.Generic;
@@ -10,6 +11,10 @@ namespace BinaryKits.Zpl.Viewer
1011
{
1112
public class ZplElementDrawer
1213
{
14+
private const int PdfDpi = 72;
15+
private const float ZplDpi = 203.2f;
16+
private const float PdfScaleFactor = PdfDpi / ZplDpi;
17+
1318
private readonly DrawerOptions _drawerOptions;
1419
private readonly IPrinterStorage _printerStorage;
1520
private readonly IElementDrawer[] _elementDrawers;
@@ -110,7 +115,13 @@ public List<byte[]> DrawMulti(
110115
// - When drawing PDF we need the Bitmap as well to fix inverted coloring
111116
Stream pdfStream = new MemoryStream();
112117
using var document = SKDocument.CreatePdf(pdfStream);
113-
using var pdfCanvas = document.BeginPage(labelImageWidth, labelImageHeight);
118+
119+
using var pdfCanvas = document.BeginPage(
120+
UnitsHelper.ConvertMillimetersToNearestInch(labelWidth) * PdfDpi,
121+
UnitsHelper.ConvertMillimetersToNearestInch(labelHeight) * PdfDpi);
122+
123+
pdfCanvas.Scale(PdfScaleFactor, PdfScaleFactor);
124+
114125
if (this._drawerOptions.PdfOutput == true)
115126
{
116127
skCanvas.AddCanvas(pdfCanvas);

0 commit comments

Comments
 (0)