2011-05-25 25 views
2

我需要能夠爲從模板創建的文檔加水印。我現在有以下代碼:水印與蝦(使用模板)

# Note: the raw PDF text (body variable below) is sent from a remote server. 
Prawn::Document.new(:template => StringIO.new(body), :page_size => 
'A4') do |document| 
    # ... including other pages and sections to the template here ... 

    # watermark 
    d.page_count.times do |i| 
    d.go_to_page i 
    d.stroke_line [d.bounds.left, d.bounds.bottom], [d.bounds.right, d.bounds.top] 
    d.draw_text "Watermark", :rotate => 45, :at => [100,100], :size => 100 
    end 
end 

這是由於某些原因我無法理解的忽略模板頁面。如果服務器添加水印,那麼此代碼將按預期工作(例如,直接Ruby代碼=在非蝦生成的頁面上沒有重疊文本,但水印在預先加水印的模板上工作)。我唯一的猜測是有一些方法可以創建服務器正在執行的z-index /圖層,但是Prawn本身不能。

下面是從做的PDF 一代本身的服務器代碼的一部分,這確實利用iText水印:

PdfStamper stamper = new PdfStamper(...); 
PdfContentByte over = stamper.GetOverContent(i + 1); 
over.BeginText(); 
over.SetTextMatrix(20, 40); 
over.SetFontAndSize(bf, 20); 
over.SetColorFill(new Color(97, 150, 58)); 
over.ShowTextAligned(Element.ALIGN_CENTER, 
        watermarkText, 
        document.PageSize.Width/2, 
        document.PageSize.Height/2, 
        55); 
over.EndText(); 
over.Stroke(); 

如果運行我的蝦用的原始數據之前,我可以水印,去 圖。

所以我的問題是:

  1. 任何人都知道我可以使用,而不是蝦的混合動力 達到同樣的效果?我寧願在本地處理水印。

  2. 對蝦中是否有與GetOverContent()基本相當的值?

  3. 有沒有更好的方法來獲取一串原始PDF數據到蝦 沒有使用:templateStringIO? (我看到#add_content方法 但沒有工作)


TL; DR:我需要浮動蝦模板文本一拉 水印的文檔上面的文字。

我會研究任何洞察力或路徑將不勝感激。如果這個 沒有任何意義,我可以澄清。

+0

**注:我知道我有,而不是「文件」,「d」 - 在實際的代碼這是一個輔助方法,因此忽略了:) – RobH 2011-05-25 13:55:51

+0

你是如何應用模板?這是一個圖像嗎? – user596916 2011-05-25 17:43:15

+0

沒有。它是一個原始的PDF數據字符串 - 這就是爲什麼我必須使用StringIO來避免將它寫入文件。 – RobH 2011-05-25 17:46:37

回答

2

您可以嘗試打印文檔。

create_stamp("watermark") do 
rotate(30, :origin => [-5, -5]) do 
    stroke_color "FF3333" 
    stroke_ellipse [0, 0], 29, 15 
    stroke_color "000000" 
    fill_color "993333" 
    font("Times-Roman") do 
    draw_text "Watermark", :at => [-23, -3] 
    end 
    fill_color "000000" 
end 
end 

stamp_at "watermark", [210, 210]