manuelbl / SwissQRBill.NET

.NET library for Swiss QR bill payment slips (aka QR-Rechnung)
MIT License
87 stars 33 forks source link

ICanvas implementation for PDFsharp (formerly known as: Embed fonts in PDF / Font Resolver) #3

Closed rhegner closed 4 years ago

rhegner commented 4 years ago

It looks like currently the font specified in BillFormat does not get embedded in the generated PDF.

Embedding fonts would be a nice feature to make sure the result looks the same on every system, especially when using less common fonts like "Liberation Sans" (which has the least permissive license of all the fonts allowed by the QR bill specification).

Ideally such an implementation would allow to provide a custom font resolver (use case: PDFs generated on a server where no fonts are available on the system). As an example, PDFsharp allows for custom font resolvers using an interface like this: https://github.com/empira/PDFsharp/blob/master/src/PdfSharp/Fonts/IFontResolver.cs

manuelbl commented 4 years ago

So far I have refrained from adding support for font embedding because I'm not convinced that the benefits would outweigh the cost.

The benefit seems minimal as Helvetica, Arial and Liberation Sans are basically identical fonts. Even a professional graphical designer can hardly distinguish them. And the layout is so simple and robust that it is flawless even if the font is exchanged for display or printing.

The only font that really differs is the Frutiger font. I wouldn't be surprised if it was just added because it is the corporate design font of PostFinance even though they hardly issue any QR bill themselves – they process bills issued by other companies.

The cost is complexity. I've already opted to go with a simple and minimalistic PDF generator because the available .NET libraries would introduce many direct and indirect dependencies that one has to deal with even if font embedding or PDF generation is not needed. And the licenses aren't fully compatible either.

Extending the current PDF generator to support font embedding is probably not worth the effort as – to the best of my knowledge – it is quite a complex matter.

An alternative is to create a separate ICanvas implementation supporting PDF font embedding and built using one of the established PDF generation libraries. In fact, there is already an ICanvas example implementation for iText, created to demonstrate how to add QR bills to an existing PDF document: IText7Canvas.cs.

Would it be an option to extend this canvas implementation or create a new one using a different library? What would the preferred PDF library be?

rhegner commented 4 years ago

Hi Manuel, thanks for your quick response! Your reasoning makes sense. I thought that maybe enabling font embedding was just a matter of providing a different setting to your PDF library. But given the complexity, and given that my concern is really more a theoretical concern and has not been a real problem so far, it makes sense to not implement that feature.

But using a customized ICanvas implementation is a really great idea! I'm using PDFsharp (because it has a much more permissive license than iText). It looks like it should be fairly simple to implement ICanvas using the transformation and drawing methods of PDFsharp.

Using this approach has another advantage: Today I use your library to generate a PDF in memory, then let PDFsharp load that byte-array, then add the rest of the invoice, then save the file. So there is a lot of unnecessary loading/saving going on. Being able to paint the QR bill on an existing canvas is much more efficient.

I will try to implement the ICanvas interface with PDFsharp and create a little example program similar to your iText example, and I will share it with you.