jung-kurt / gofpdf

A PDF document generator with high level support for text, drawing and images
http://godoc.org/github.com/jung-kurt/gofpdf
MIT License
4.33k stars 782 forks source link

Add SetTextRenderingMode function #331

Closed nickjwhite closed 4 years ago

nickjwhite commented 4 years ago

This follows section 9.3.6 of the PDF32000_2008 specification.

I use this to create "invisible" text for overlaying on images, to make searchable PDFs from OCR results.

I also made a variant branch of this with consts for the different possible modes, addtextrenderingmode-withconsts. I wasn't sure which would be a more fpdf way of doing it, so made both versions.

jung-kurt commented 4 years ago

Nice!

Is a certain minimum document version required to use this? If so, you will need make sure the .pdfVersion field is properly updated; see this code.

nickjwhite commented 4 years ago

Good question, I hadn't thought of that!

I just did some searching, and it has been available since at least PDF version 1.3; it's documented in Section 5.2.5 of https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/pdf_reference_archives/PDFReference.pdf

I just thought, should I add a test for this? It would have to be of the type: generate a document with different text rendering modes, compare to a pregenerated golden copy. At least I can't think of another way to test it. That said, it's simple enough that I can't really imagine anything going wrong with it that testing would catch... (famous last words). I'm happy to write up a test and add it, if you think that would be worthwhile, though.

jung-kurt commented 4 years ago

I just did some searching, and it has been available since at least PDF version 1.3

Good -- no need for a version check then. (1.3 is the default minimum required version for gofpdf.)

I just thought, should I add a test for this? It would have to be of the type: generate a document with different text rendering modes, compare to a pregenerated golden copy.

One would think that gofpdf would generate, for a given code sequence, exact copies of a document suitable for comparison. But after fussing with embedded timestamps and stable-sorted catalogs, subtle differences between Go versions and platforms still sometimes results in different output. So in this case, I am fine merging this without a test.

This is a welcome feature -- thanks for the PR. I'll get to it soon.

nickjwhite commented 4 years ago

Great, thanks @jung-kurt, I'm happy it's a welcome feature.

And thanks so much for gofpdf in general, I just recently found it and it's great :) I was deciding between this and the unipdf package, but chose gofpdf because it looked nicely designed to be easy to hack on, and I'd prefer a proper community driven project, and I'm glad I did.

jung-kurt commented 4 years ago

Thanks, @nickjwhite. I wound up making a small example to demonstrate your routine. Feel free to extend it with the clip modes if you like.