Skip to content

Commit

Permalink
Fix PDF scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshmul1 committed Jun 12, 2024
1 parent 4a039ce commit cfda683
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/BinaryKits.Zpl.Viewer/Helpers/UnitsHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace BinaryKits.Zpl.Viewer.Helpers
{
static internal class UnitsHelper
{
static internal double ConvertMillimetersToInches(double labelWidth)
{
return labelWidth / 25.4;
}
}
}
13 changes: 12 additions & 1 deletion src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -110,7 +115,13 @@ public List<byte[]> 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);
Expand Down

0 comments on commit cfda683

Please sign in to comment.