English 中文(简体)
Generate PDF base64 encode from iText in Java
原标题:Generate PDF base64 encode from iText in Java

我怎么能将伊塔特地区生成的PDF文档编成基64?

这是我的(java)法典。

App.java

public class App {

public static void main(String[] args) throws DocumentException, IOException {
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("pdf.pdf"));
    // step 3
    document.open();

    String str = "<html><head></head><body style="font-size:12.0pt; font-family:Times New Roman">"
            + "<a href= http://www.rgagnon.com/howto.html ><b>Real s HowTo</b></a>" + "<h1>Show your support</h1>"
            + "<p>It DOES cost a lot to produce this site - in ISP storage and transfer fees</p>"
            + "<p>TEST POLSKICH ZNAKÓW: u0104u0105u0106u0107u00d3u00f3u0141u0142u0179u017au017bu017cu017du017eu0118u0119</p>"
            + "<hr/>"
            + "<p>the huge amounts of time it takes for one person to design and write the actual content.</p>"
            + "<p>If you feel that effort has been useful to you, perhaps you will consider giving something back?</p>"
            + "<p>Donate using PayPalu017d</p>" + "<p>Contributions via PayPal are accepted in any amount</p>"
            + "<p><br/><table border= 1 ><tr><td>Java HowTo</td></tr><tr>"
            + "<td style= background-color:red; >Javascript HowTo</td></tr>"
            + "<tr><td>Powerbuilder HowTo</td></tr></table></p>" + "</body></html>";

    XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
    InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));

    worker.parseXHtml(writer, document, is, Charset.forName("UTF-8"));

    // step 5
    document.close();

    System.out.println("PDF Created!");

}

}

iText 图书馆用户(xmlworker e itextpdf)

www.un.org/Depts/DGACM/index_spanish.htm Pom file:

    <dependencies>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf.tool</groupId>
        <artifactId>xmlworker</artifactId>
        <version>5.4.1</version>
    </dependency>
</dependencies>

任何帮助都受到欢迎。

问题回答

我不得不做同样的事情,这就是我是如何做的。 我利用阿普切特图书馆编造密码,将内容编成一栏。 希望这一帮助。

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;

File file = new File("test.pdf");

  // write your html template to the file and close the document.
byte[] pdf =FileUtils.readFileToByteArray(file);
byte[] encoded = Base64.encodeBase64(pdf); 
      //or byte[] encoded = Base64.encodeBase64(file.getBytes());

这是用粗体字做的,根据你的询问写了适当的代码。

使用标准java。 基地64:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, baos);

byte[] bytes = baos.toByteArray();
bytes = Base64.getEncoder().encode(bytes);
Files.write(Paths.get("pdf.pdf"), bytes);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签