0

When I transform html to pdf , I set enconding utf-8,when I convert pdf, long Engish words or long numbers can not wrap,but chinese can.

I am confused this problem several hours.

This is my html file:

There are some chinese in html because is just a test. you can ignore it

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>award system</title>
    <meta name="description" content="" />
    <link rel="stylesheet" href="part.css">
</head>
<body class="page-header-fixed">
<h2 class="part-title">七  Certificate Catalogue of Core intellectual     property</h2>      
<table style="table-layout:fixed;white-space:normal;word-wrap:break-word;word-break:break-all;" >
  <tbody>

      <tr >
        <td class="table-item" data-name="type">Invention Patent</td>
        <td class="table-item" data-name="name">一种访问网站的方法和系统</td>
        <td class="table-item" data-name="country">chinese</td>
        <td class="table-item" data-name="auth">ZL201310169965.9</td>
        <td class="table-item" data-name="date">2017-11-03</td>
        <td class="table-item" ><p style="white-space:normal;word-wrap:break-word;word-break:break-all;">2680796</p></td>
        <td class="table-item" data-name="obligee">www.ctbri.commmmmmmmmmmmmm</td>
        <td class="table-item" data-name="inventor">name</td>
        <td class="table-item" data-name="status">Authorized</td>
      </tr>

  </tbody>
</table>
</body>
</html>

This is css file

body {
  background: none !important;
}

.title {
  text-align: center;
  font-size: 24px;
}

.subtitle {
  text-align: center;
  font-size: 16px;
}

.part-title {
  text-align: center;
  font-size: 18px;
}

table {
  width: 80%;
  border-collapse: collapse;
  border-left: 1px solid #000;
  margin: 0 auto 50px;
}

table td {
  border: 1px solid #000;
  border-left: none;
  height: 40px;
  text-align: center;
  font-size: 14px;
}

table .item-name {
  width: 20%;
}

table .align-left {
  text-align: left;
}

table p {
  margin: 0;
}

This is my java code:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.css.media.MediaDeviceDescription;
import com.itextpdf.html2pdf.css.media.MediaType;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.WriterProperties;
import com.itextpdf.layout.font.FontProvider;

public class Pdf0010HtmlToPdf {

    public static void main(String[] args) {

        // baseuri:the parent path of html/css/javasript
        String baseuri = "G:/TPractice/workOne/TransforePdf/src/main/resources/pdf0010htmlTopdf/";
        // html source file
        String desc = "G:/TPractice/workOne/TransforePdf/src/main/resources/pdf0010htmlTopdf/problem.pdf";
        File htmlSource = new File(baseuri + "problem.html");

        // pdfHTML specific code
        ConverterProperties converterProperties = new ConverterProperties();
        try {
            FileOutputStream outputStream = new FileOutputStream(desc);
            WriterProperties writerProperties = new WriterProperties();
            writerProperties.addXmpMetadata();


            PdfWriter writer = new PdfWriter(outputStream);

            PdfDocument pdf = new PdfDocument(writer);
            pdf.setTagged();
            PageSize pageSize = new PageSize(575, 1500);
            pdf.setDefaultPageSize(pageSize);
            FontProvider font = new FontProvider();
            font.addStandardPdfFonts();
            font.addFont("C:\\Windows\\Fonts\\simhei.ttf");// set my font as chinese font 

            converterProperties.setFontProvider(font);
            converterProperties.setBaseUri(baseuri);
            MediaDeviceDescription mediaDeviceDescription = new MediaDeviceDescription(MediaType.SCREEN);
            converterProperties.setMediaDeviceDescription(mediaDeviceDescription);
            HtmlConverter.convertToPdf(new FileInputStream(htmlSource), pdf, converterProperties);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("OK!");
    }
}   

1 Answer 1

4
//HtmlConverter.convertToPdf(new FileInputStream(htmlSource), pdf, converterProperties);//change this use below

    List<IElement> elements =HtmlConverter.convertToElements(new FileInputStream(htmlSource), converterProperties);
    Document document = new Document(pdfDoc);
    document.setProperty(Property.SPLIT_CHARACTERS,new DefaultSplitCharacters(){
        @Override
        public boolean isSplitCharacter(GlyphLine text, int glyphPos) {
//return super.isSplitCharacter(text, glyphPos);//override this 
            return true;//解决word-break: break-all;不兼容的问题
        }
    });
    for (IElement element : elements) {
        document.add((IBlockElement)element);
    }
    document.close();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.