Skip to content

Commit cfda683

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

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace BinaryKits.Zpl.Viewer.Helpers
2+
{
3+
static internal class UnitsHelper
4+
{
5+
static internal double ConvertMillimetersToInches(double labelWidth)
6+
{
7+
return labelWidth / 25.4;
8+
}
9+
}
10+
}

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+
(float)(UnitsHelper.ConvertMillimetersToInches(labelWidth) * PdfDpi),
121+
(float)(UnitsHelper.ConvertMillimetersToInches(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)