Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PDF scaling #251

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading