1

I'm trying to generate PDF file on android with some Chinese characters using below code.It is working fine without Chinese characters(Only English) and PDF size is around 40-50Kb. But when I try to generate PDF with Chinese characters ,it is working and PDF size is 3-4MB.

Number of characters for both cases are almost same. Any idea how to reduce the PDF size? Because i need to upload the PDF to server.

public void generatePDFDoc(Activity activity, View view, String pdfFileName){

    {
        File pdfFile = null;
        PrintAttributes printAttrs = new PrintAttributes.Builder().
                setColorMode(PrintAttributes.COLOR_MODE_COLOR).
                setMediaSize(PrintAttributes.MediaSize.NA_LETTER).
                setResolution(new PrintAttributes.Resolution("print", activity.PRINT_SERVICE,
                        25, 25)).
                setMinMargins(PrintAttributes.Margins.NO_MARGINS).
                build();

        PdfDocument document = new PrintedPdfDocument(activity, printAttrs);
        View content = view;
        int width = content.getWidth();
        int height = content.getHeight();
        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(width, height, 1).create();
        PdfDocument.Page page = document.startPage(pageInfo);
        content.draw(page.getCanvas());
        document.finishPage(page);
        try {
            pdfFile = new File(pdfFileName);
            if (pdfFile != null) {
                FileOutputStream os = new FileOutputStream(pdfFile);
                document.writeTo(os);
                document.close();
                os.close();
            }
        } catch (Exception e) {
            throw new RuntimeException("Error generating file", e);
        } finally {
            if (pdfFile != null) {
                //Success Generate PDF file

            }
        }


    }
}  
L. Swifter
  • 3,179
  • 28
  • 52
Ramindu Weeraman
  • 344
  • 2
  • 10
  • Have you tried with UTF-8 encoding? http://stackoverflow.com/a/9853261/5281666 – Milos Lulic Sep 21 '16 at 02:10
  • Thanks.How can we add UTF-8 to FileOutputStream ? Because document.writeTo(os); expecting FileOutputstream as parameter. – Ramindu Weeraman Sep 21 '16 at 02:39
  • BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("jedis.txt"), "UTF-8")); – Milos Lulic Sep 21 '16 at 03:26
  • Thanks.Issue is here, document.writeTo(os);..I need FileOutputStream with UTF encoding. not Writer.writeTo method accept only OutputStream not writer. – Ramindu Weeraman Sep 21 '16 at 03:34
  • Actually, i have found the issue, Chinese characters are printing like an images not like character.This is the reason for increase PDF file size.But still i don't have workaround to fix this. – Ramindu Weeraman Sep 21 '16 at 09:30
  • I had encountered this issue. Do you have any workaround to fix it now? – Pogo Lin Aug 22 '17 at 07:29
  • Yes. I have fixed this. You need to use chinese font inside your application.copy chinese font it to raw folder and set all Textview/Editview to that font.I used simsun.ttc – Ramindu Weeraman Aug 24 '17 at 02:50

0 Answers0