使用 libharu 和 vala 创建 pdf

发布于 2024-12-10 07:40:48 字数 198 浏览 0 评论 0原文

我想知道是否有人可以指导我如何从 vala 语言动态创建 pdf 文件(无需将文件另存为 pdf)。我听说这可以用 libharu 来完成,所以我一直在研究他们的文档,但对我来说仍然有点模糊。有谁知道如何...

从使用 vala 创建的 UI 发送书面注释/文本到 libharu?并让libharu从中创建一个pdf文件?

非常感谢您的帮助。谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌逼全场 2024-12-17 07:40:48

即使问题很老,我也需要类似的东西......

正如 nemequ 所说,你需要编写一个 vapi 来包装库。

这是一个基于 libhpdf 2.0.8(不是最新的)的最小版本

=== 文件名: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 {
    }
}

然后你可以从 vala 使用它。

=== 文件名: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;
}

编译(在 Windows 上尝试过,您需要调整路径):

valac -C --save-temps text_demo.vala --vapidir 。 --pkg 哈鲁

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

并运行:

text_demo.exe test.pdf

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文