2011-10-16 62 views
1

我想知道是否有人可以給我指示如何從vala語言動態創建pdf文件(而不必將文件另存爲pdf)。我聽說它可以用libharu完成,所以我一直在查看他們的文檔,但它仍然有點朦朧。有沒有人知道如何去...用libharu和vala創建pdf

從用vala創建的用戶界面發送書面註釋/文本到libharu?並libharu從它創建一個PDF?

幫助將不勝感激。謝謝!

+0

Unfortunatel y libharu,AFAIK,沒有Vala綁定(即,vapi)。有可能創建一些,並不是太難(至少與其他語言相比)。有關示例,請參閱http://git.gnome.org/browse/vala/tree/vapi。 這個問題的答案取決於libharu綁定。 – nemequ

回答

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上,您需要調整的路徑):

華劣克-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-位字段-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