Hi, 

Thanks for the fast response.

I would like to replace a segment [[MY_NAME]] with chinese, european or korean 
name depending.
How would i be able to replace the [[MY_NAME]] with the UTF8 name and still be 
able to convert to PDF?
I have tried for some time only to get rubbish name.

I have attached my codes and html for reference.

Thank you for your assistance.

Thanks.



----- Original Message -----
From: "Balder" <[email protected]>
To: [email protected]
Sent: Thursday, May 26, 2011 6:30:22 PM
Subject: Re: [iText-questions] Converting UTF8 in HTML to PDF


On 26/05/2011 11:50, Wen Kai Ng wrote: 

Hi,

Is it possible to convert HTML with chinese and european characters in UTF8 to 
PDF? You need a font that supports the characters you want to put in the PDF. 
If you have that you can display anything. You could even make your own font. 
@see Chapter 2 point 2.2.2 (page 26) of iText in Action 2 for explanations on 
UTF-8 

How you read the characters as bytes from your source is up to you and depends 
on your programming language/system 


Could you please kindly provide some sample codes for this? Read a string as 
UTF-8 in java: 

String str = new String(yourbytes, "UTF-8"); 


Thanks. 

-- 


Kind Regards 
Balder 
redlab-log 
@redlabbe 

<html>
<head>
    </head>
    <body>
        <table>
            <tr>
                <td colspan="2"><b>Application Form</b></td>
            </tr>
            <tr>
                <td colspan="2"><br/></td>
            </tr>
            <tr>
                <td colspan="2"><b><i>Domain Name: [[MY_NAME]]</i></b></td>
            </tr>
            <tr>
                <td colspan="2"><br/></td>
            </tr>

            <tr>
                <td colspan="2"><b><i>Info</i></b></td>
            </tr>
            <tr>
                <td width="30%">Name:</td>
                <td>Smoothie Yogurt</td>
            </tr>
            <tr>
                <td>Organisation:</td>
                <td></td>
            </tr>
            <tr>
                <td>Address:</td>
                <td>Suite 123</td>
            </tr>
            <tr>
                <td>Tel:</td>
                <td>+372.7654321</td>
            </tr>
            
        </table>
    </body>
</html>
package alutester;

import java.io.FileInputStream;
import org.xml.sax.helpers.DefaultHandler;

import com.itextpdf.text.Document;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.FontSelector;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.StringReader;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.html.simpleparser.StyleSheet;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import com.itextpdf.text.Element;
import java.io.IOException;
import java.io.File;

//public class Peace extends DefaultHandler {
public class Test2 extends DefaultHandler {

    /** The resulting PDF file. */
    public static final String RESULT = "resources/results/peace.pdf";
    /** The XML file with the text. */
    public static final String RESOURCE = "resources/xml/peace.xml";
    /** Paths to and encodings of fonts we're going to use in this example */
    public static String[][] FONTS = {
        {"resources/fonts/arialuni.ttf", BaseFont.IDENTITY_H},
        {"resources/fonts/abserif4_5.ttf", BaseFont.IDENTITY_H},
        {"resources/fonts/damase.ttf", BaseFont.IDENTITY_H},
        {"resources/fonts/fsex2p00_public.ttf", BaseFont.IDENTITY_H}
    };
    /** Holds he fonts that can be used for the peace message. */
    public FontSelector fs;
    /** The columns that contains the message. */
    protected PdfPTable table;
    /** The language. */
    protected String language;
    /** The countries. */
    protected String countries;
    /** Indicates when the text should be written from right to left. */
    protected boolean rtl;
    /** The StringBuffer that holds the characters. */
    protected StringBuffer buf = new StringBuffer();

    /**
     * Creates the handler for the pace.xml file.
     */
    public Test2() {
        fs = new FontSelector();
        for (int i = 0; i < FONTS.length; i++) {
            fs.addFont(FontFactory.getFont(FONTS[i][0], FONTS[i][1], BaseFont.EMBEDDED));
        }
    }

    public static void main(String[] args) {
        Test2 t = new Test2();
        t.convertHTMLToPDFUTF8("", "");
    }

    public boolean convertHTMLToPDFUTF8(String htmlString, String pdfPath) {
        File file = new File("resources/xml/testUNI.txt");
        int fileLen = (int) file.length();
        
        StringReader stringReader=null;
        try {
            FileInputStream fis = new FileInputStream(file);
            byte[] bytes = new byte[fileLen];
            fis.read(bytes, 0, fileLen);
            String userHTML = new String(bytes, "UTF-8");
            stringReader = new StringReader(userHTML);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        Document document = new Document();
        StyleSheet st = new StyleSheet();
        try {
            PdfWriter.getInstance(document, new FileOutputStream("resources/results/test.pdf"));
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        document.open();
        List p;
        try {
            p = HTMLWorker.parseToList(stringReader, st);
            for (int k = 0; k < p.size(); ++k) {
                try {
                    document.add((Element) p.get(k));
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        document.close();
        return true;
    }
}

------------------------------------------------------------------------------
vRanger cuts backup time in half-while increasing security.
With the market-leading solution for virtual backup and recovery, 
you get blazing-fast, flexible, and affordable data protection.
Download your free trial now. 
http://p.sf.net/sfu/quest-d2dcopy1
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to