PDF generation in D?

Vasudev Ram via Digitalmars-d digitalmars-d at puremagic.com
Tue Nov 22 10:49:51 PST 2016


On Thursday, 10 November 2016 at 22:30:34 UTC, Karabuta wrote:
> Hello community, does anyone have on something for PDF 
> generation in D? I may need a PDF generation library in a 
> vibe.d project I'm working on. :)

Hi,

I did read all the replies posted up to now. Posting a few 
alternative methods I thought of, some of which involve calling C 
libraries from D - not sure how suitable they will be for your 
specific needs, some checking will be required:

- check out libharu - it is an open source C library for PDF 
generation. If you can call it from D, it may work for your needs.

http://libharu.org/ . Libharu needs a new maintainer now, but the 
site says it still works.

- check out PDFlib(.com). PDFlib is a paid product, and the core 
is a C library. However it has an open source version IIRC, and 
may be free for personal use (not sure if you want this for 
personal or commercial use). Again, would need to call it's (C) 
functions from D. PDFlib is a mature product which has been 
around for many years. It also has binding to some other 
languages.

(I've tried both the above libs at least a bit, and they do work.)

- this one is an obvious, though roundabout method: if the D- and 
C-based ones are not suitable for whatever reason, there are 
generic methods applicable to calling (a program that uses) a 
PDF-generation library (or any library for that matter) in any 
other language, such as via XML-RPC (if D has a client library 
for that, or if D can call a C client XML-RPC library), REST or 
sockets.

- an even simpler method than above (though, of course, less 
efficient for multiple calls) may be to shell out to some 
executable [1] written in another language which has a PDF 
generation library, pass the necessary inputs on the command line 
(as command-line options,  an input file name, and an output PDF 
filename.

[1] By executable here, I don't only mean a compiled user 
executable such as a C or C++ or Java app. It could also be a 
call to a language interpreter (the executable) taking a script 
in that language, as an argument to run, and the script name 
could be followed by arguments for the script itself (e.g. python 
pdf_gen_prog.py arg1 arg2 ...) . Using this approach, for 
example, one could shell out to Python, run a Python script that 
uses ReportLab, and use that to do the job, since ReportLab is 
fairly powerful for PDF generation, though a bit low-level. 
However, it does have things like Paragraphs, Stories, Styles, 
and Platypus which are a bit higher-level. And if your PDF output 
involves only text (i.e. no images, charts, varying fonts, etc.), 
then you can even consider shelling out to a Python program you 
write, that uses xtopdf - which is my PDF generation toolkit 
written in Python, which uses ReportLab internally, and provides 
a somewhat higher abstraction for a subset of ReportLab's 
functionality, namely generation of text-only line-oriented PDF 
output, with automatic headers, footers, page numbering and 
pagination). xtopdf is quite easy to use: With low-level 
Reportlab features, you have to write your PDF generation logic 
in terms of operations on a Canvas object, not lines of text, so 
you have to say things like writeString(x, y, string), and 
calculate each x and y, reset the font to the same value after 
each new page (a limitation), but with xtopdf you get the higher 
level abstraction of something like a text file (a PDFWriter 
object), and you just write lines of text to the PDFWriter object 
using its writeLine(string) method, until you are done. Just have 
to set the header and footer and font once, first (3 lines for 
that). Total for a simple file is under 10 or so lines of Python 
code, to generate a PDF from text input, using xtopdf - with some 
amount of simple customized formatting of the text possible, in 
terms of left-or-right-justifying, centering, etc., using 
Python's easy string handling, with a few more lines of code.

ReportLab main site: http://reportlab.com

ReportLab open source version: http://reportlab.com/ftp

Good high-level overview of xtopdf: 
http://slides.com/vasudevram/xtopdf (including uses, users, 
supported input formats, supported platforms, example programs, 
etc.)

xtopdf on Bitbucket: https://bitbucket.org/vasudevram/xtopdf

xtopdf examples on my blog: 
http://jugad2.blogspot.com/search/label/xtopdf

Guide to installing and using xtopdf:

http://jugad2.blogspot.in/2012/07/guide-to-installing-and-using-xtopdf.html

HTH,
Vasudev
jugad2.blogspot.com
vasudevram.github.io




More information about the Digitalmars-d mailing list