我想知道是否有人可以指导我如何从 vala 语言动态创建 pdf 文件(无需将文件另存为 pdf)。我听说它可以用 libharu 完成,所以我一直在查看他们的文档,但对我来说仍然有点模糊。有没有人知道怎么去...
从使用 vala 创建的 UI 向 libharu 发送书面注释/文本?并让libharu从中创建一个pdf?
帮助将不胜感激。谢谢!
即使问题很老,我也需要类似的东西......
正如 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 测试.pdf