2012-12-06 44 views
0

我需要在pdf文檔中打印土耳其字符,如「ş」,「ç」,「ü」等。 我正在使用以下代碼。Salesforce中Blob對象的toPdf方法的字符集問題

global class InvoicePDFGenerator { 

    public static final String FORM_HTML_START = '<HTML><BODY>'; 
    public static final String FORM_HTML_END = '</BODY></HTML>'; 

    webservice static void generateInvoicePDF(String invoiceId){ 
     OppoInvoice__c invoice= [SELECT Id,Account_Name__c FROM OppoInvoice__c WHERE Id=:invoiceId]; 
     String pdfContent = ''; 
     try { 
      pdfContent = '<html><head><meta http-equiv=content-type content=text/html;charset=iso-8859-9></meta></head><body>'; 

      pdfContent = pdfContent + '<P>' + invoice.Account_Name__c+ '</P>'; 
      pdfContent = pdfContent + FORM_HTML_END; 
     }catch(Exception e){ 
      pdfContent = '' + FORM_HTML_START; 
      pdfContent = pdfContent + '<P>THERE WAS AN ERROR GENERATING PDF: ' + e.getMessage() + '</P>'; 
      pdfContent = pdfContent + FORM_HTML_END; 
     } 

     Attachment attachmentPDF = new Attachment(); 
     attachmentPDF.parentId = invoice.Id; 
     attachmentPDF.Name = 'Invoice.pdf'; 
     attachmentPDF.body = Blob.toPDF(pdfContent); //This creates the PDF content 
     insert attachmentPDF; 

    } 
} 

我覺得從attachmentPDF.body = Blob.toPDF(pdfContent)行引起的問題。你對這個問題有什麼想法嗎?

+1

會發生什麼?我假設Salesforce在其字符串中使用UTF-8。你有沒有嘗試拋棄試圖設置'charset = iso-8859-9'的'META'標籤? – tomlogic

+0

我也嘗試過使用UTF-8,但結果沒有改變。代碼工作正常,但土耳其人不會出現。例如,如果pdfContent變量的值是'abcşidça',則pdf文檔的輸出是'abcida'。 它如何編寫一個元標籤,沒有?你能告訴我一個例子嗎? – mustafatop

+0

(1)您可以發佈不使用查詢的代碼版本嗎?換句話說,你能否將一個帶有土耳其字符的字符串保持爲pdfContent。這將允許其他人(如我)以不同的條件嘗試Blob.toPDF()。 (2)您可以使用這些相同的土耳其字符創建html文件,並驗證此文件是否可以在瀏覽器中使用。 – Bryan

回答

0

這可能是您應該向Salesforce報告的Blob.toPDF方法的限制。

我能想出的最接近的解決方案是對每個字符串使用escapeHtml4()方法,但只包含字符實體的一個子集。它將您的示例字符串'abcşidça'轉換爲'abcşid&ccedil;a'

如果meta標記影響輸出,你可以試着用<html><body>開始HTML,看看是否有幫助。

0

我將一個帶有土耳其字符的字符串常量壓入pdfContent。輸出是'öçşğüıÖÇŞĞÜİ'的輸入值'öçüÖÇÜ' 。

這是沒有查詢的代碼。

global class AccountPDFGenerator 
{ 

    webservice static void generateInvoicePDF(String accountId) 
    { 
//  Account account = [SELECT Id,Name FROM Account WHERE Id=:accountId]; 

     String accId = accountId; 

     String pdfContent = ''; 
     try 
     { 
      pdfContent = '<html><head><meta http-equiv="content-type" content="text/html; charset=iso-8859-9"></meta></head><body>';   
      pdfContent = pdfContent + '<p style="color:red">' + 'öçşğüıÖÇŞĞÜİ' + '</p>'; 
      pdfContent = pdfContent + '</body></html>'; 
     }catch(Exception e) 
     { 
      pdfContent = pdfContent + '<P>THERE WAS AN ERROR GENERATING PDF: ' + e.getMessage() + '</P>'; 
     } 
     pdfContent = 'öçşğüıÖÇŞĞÜİ'; 

     Attachment attachmentPDF = new Attachment(); 
     attachmentPDF.parentId = accId; 
     attachmentPDF.Name = 'Invoice.pdf'; 
//  attachmentPDF.body= Blob.valueOf(pdfContent); 
     attachmentPDF.body = Blob.toPDF(pdfContent); //This creates the PDF content 
     insert attachmentPDF; 

    } 

} 

這創造了自定義按鈕的javascript代碼,其中設置>自定義>帳戶>按鈕和鏈接

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")} 
sforce.apex.execute("AccountPDFGenerator","generateInvoicePDF", {id:"{!Account.Id}"}); 
window.alert("Account Id is sent."); 

這是HTML文件內容

<html><head><meta http-equiv=content-type content=text/html;charset=iso-8859-9></meta></head><body><p>öçşğüıÖÇŞĞÜİ</p></body></html>