我想知道是否有人可以给我指点如何动态创建pdf文件(无需将文件保存为pdf)来自vala语言。我听说它可以用libharu完成所以我一直在查看他们的文档,但它仍然有点模糊我。有谁知道怎么去...
从使用vala创建的UI发送书面注释/文本到libharu?并且libharu从中创建了一个pdf?
帮助将不胜感激。谢谢!
答案 0 :(得分:3)
即使问题很老,我也需要类似的东西......
正如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 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
跑步:
text_demo.exe test.pdf