creating pdfs with libharu and vala

1k Views Asked by At

i was wondering if anyone could give me pointers on how to go about creating pdf files dynamically(without having to save the file as a pdf) from the vala language. i heard it can be done with libharu so ive been looking into their documentation but its still kinda hazy for me. does anyone know how to go about...

sending written annotations/text from a UI created with vala, to libharu? and having libharu create a pdf from it?

help would be much appreciated. thanks!

1

There are 1 best solutions below

0
On

Even if the question is quite old I was in need of something similar...

As nemequ said you need to write a vapi to wrap the library.

This is a minimal one based on libhpdf 2.0.8 (not the latest)

=== Filename: haru.vapi ===

[CCode(cheader_filename="hpdf.h", cprefix="HPDF_")]
namespace HPDF {
    [CCode(cname="HPDF_STATUS")]
    public struct Status : ulong {
    }

    [CCode(cname="HPDF_REAL")]
    public struct Real : float {
    }

    [CCode(cname="HPDF_Error_Handler", instance_pos = -1)]
    public delegate void ErrorHandler (Status error_no, Status detail_no);

    [Compact]
    [CCode(free_function="HPDF_Free", cname="HPDF_Doc")]
    public class Doc {
        [CCode(cname="HPDF_New", instance_pos = -1)]
        public Doc (ErrorHandler error_handler);

        [CCode(cname="HPDF_AddPage")]
        public unowned Page add_page();

        [CCode(cname="HPDF_GetFont")]
        public unowned Font get_font(string name, string? encoding = null);

        [CCode(cname="HPDF_SaveToFile")]
        public Status save_to_file (string file);
    }

    [Compact]
    [CCode(cname="HPDF_Page")]
    public class Page {
        [CCode(cname="HPDF_Page_SetFontAndSize")]
        public Status set_font_and_size (Font font, float size);

        [CCode(cname="HPDF_Page_BeginText")]
        public Status begin_text ();

        [CCode(cname="HPDF_Page_EndText")]
        public Status end_text ();

        [CCode(cname="HPDF_Page_TextOut")]
        public Status text_out (Real x, Real y, string chars);

        [CCode(cname="HPDF_Page_SetCharSpace")]
        public Status set_char_space (Real value);

        [CCode(cname="HPDF_Page_SetWordSpace")]
        public Status set_word_space (Real value);
    }

    [Compact]
    [CCode(cname="HPDF_Font")]
    public class Font {
    }
}

Then you can consume it from vala.

=== Filename: text_demo.vala ===

using HPDF;

public class Demos.TextDemo {
    private void error_handler (Status error_no, Status detail_no) {
        stderr.printf("Error %d - detail %d\n", (int)error_no, (int)detail_no);
    }

    public void run (string filename) {
        string samp_text2 = "The quick brown fox jumps over the lazy dog.";

        Doc pdf = new Doc(this.error_handler);
        unowned Page page = pdf.add_page ();
        unowned Font font;

        font = pdf.get_font ("Helvetica");

        page.set_font_and_size (font, 24);

        /* char-spacing 0 */
        page.begin_text ();
        page.text_out (60, 140, samp_text2);
        page.end_text ();

        /* char-spacing 1.5 */
        page.set_char_space (1.5f);

        page.begin_text ();
        page.text_out (60, 100, samp_text2);
        page.end_text ();

        /* char-spacing 1.5, word-spacing 3.5 */
        page.set_word_space (2.5f);

        page.begin_text ();
        page.text_out (60, 60, samp_text2);
        page.end_text ();

        /* save the document to a file */
        stderr.printf("Writing pdf to: %s\n", filename);
        pdf.save_to_file (filename);
    }
}

public static int main(string[] args) {
    new Demos.TextDemo().run (args[1]);

    return 0;
}

To compile (tried on windows you'll need to tweak the paths):

valac -C --save-temps text_demo.vala --vapidir . --pkg haru

gcc -g -o text_demo.exe text_demo.c -I ./libharu-2.0.8/include -L libharu-2.0.8 -l libhpdf -mms-bitfields -IC:/Src/Tools/opt/include/glib-2.0 -IC:/Src/Tools/opt/lib/glib-2.0/include -LC:/Src/Tools/opt/lib -lglib-2.0 -lintl -lgobject-2.0

And to run:

text_demo.exe test.pdf