账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    关于jsoup格式化html 生成pdf 文本换行问题。
    42
    0

    最近在做一个功能,其中有一部分涉及到word转pdf,我是word转成html 之后 再转成pdf的,在html转换pdf的时候,html里面的长文本几十个字那种,在一个span标签里面,生成的pdf 这文字他不换行,我用css强制换行也没用,貌似itext只识别换行标签或者手动回车。 请问这个怎么解决一下 文字换行问题呢?
    File f = new File(targetHtml);

        org.jsoup.nodes.Document doc = Jsoup.parse(f, "UTF-8");
        doc.select("meta").removeAttr("name");
        doc.select("meta").attr("content", "text/html; charset=UTF-8");
        doc.select("meta").attr("http-equiv", "Content-Type");
        doc.select("meta").html("&nbsp");
        doc.select("img").html("&nbsp");
        doc.select("style").attr("mce_bogus", "1");
        doc.select("style").html(".divcss5{word-wrap:break-word} ");
        doc.select("body").attr("style", "font-family:'SimSun'");
        doc.select("span").attr("style", "font-family:'SimSun'");
        doc.select("div").attr("style", "width:30%;");
        doc.select("div").attr("class", "divcss5");
        doc.select("br").append(" ");
        doc.select("a").append(" ");
        doc.select("html").before("<?xml version='1.0' encoding='UTF-8'>");
        /*
         * Jsoup只是解析,不能保存修改,所以要在这里保存修改。
         */
        FileOutputStream fos = new FileOutputStream(f, false);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
        osw.write(doc.html());
        System.out.println(doc.html());
        osw.close();
    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 飘过4000米舔包 普通会员 1楼

      在使用Jsoup格式化HTML并生成PDF文本时,如果文本换行问题,可以使用以下方法来解决:

      1. 使用replace()方法:这个方法可以用来替换字符串中的某个字符。在处理HTML文本时,可以使用这个方法来实现换行。例如:

      java String html = "<p>This is a sample text that needs to be formatted and converted to a PDF document.</p>"; String pdf = html.replace("\n", "<br>");

      在这个例子中,"\n"被替换为"<br>",从而实现了换行。

      1. 使用split()方法:这个方法可以用来将字符串按照指定的分隔符分割成多个子字符串。在处理HTML文本时,可以使用这个方法来实现换行。例如:

      java String html = "<p>This is a sample text that needs to be formatted and converted to a PDF document.</p>"; String pdf = html.split("\n");

      在这个例子中,"\n"被分割成了多个子字符串,每个子字符串都被替换为"<br>",从而实现了换行。

      1. 使用replaceAll()方法:这个方法可以用来替换字符串中的某个子字符串。在处理HTML文本时,可以使用这个方法来实现换行。例如:

      java String html = "<p>This is a sample text that needs to be formatted and converted to a PDF document.</p>"; String pdf = html.replaceAll("\n", "<br>");

      在这个例子中,"\n"被替换为"<br>",从而实现了换行。

      注意:以上方法都会将文本中的换行符替换为制表符,如果需要替换所有的换行符,可以使用replaceAll()方法,例如:

      java String html = "<p>This is a sample text that needs to be formatted and converted to a PDF document.</p>"; String pdf = html.replaceAll("\\n", "<br>");

      在这个例子中,"\n"被替换为"<br>",从而实现了所有的换行符。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部