日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
itext7html轉(zhuǎn)pdf圖片寬高

在Java開(kāi)發(fā)中,我們經(jīng)常需要將HTML內(nèi)容轉(zhuǎn)換為PDF格式,iText7是一個(gè)非常強(qiáng)大的庫(kù),可以幫助我們實(shí)現(xiàn)這一目標(biāo),在iText7中,我們可以使用HtmlConverter類來(lái)將HTML轉(zhuǎn)換為PDF,當(dāng)我們處理包含圖片的HTML時(shí),可能會(huì)遇到一些問(wèn)題,比如圖片的寬高不正確,這是因?yàn)镠TML和PDF的渲染方式不同,HTML是矢量圖形,而PDF是位圖,我們需要進(jìn)行一些額外的處理,以確保圖片在PDF中的寬高正確。

以下是一個(gè)簡(jiǎn)單的示例,展示了如何使用iText7將HTML轉(zhuǎn)換為PDF,并設(shè)置圖片的寬高:

import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.property.UnitValue;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
public class HtmlToPdf {
    public static void main(String[] args) throws IOException {
        String htmlPath = "path/to/your/html/file";
        String pdfPath = "path/to/your/pdf/file";
        // 創(chuàng)建PdfWriter實(shí)例
        PdfWriter writer = new PdfWriter(new FileOutputStream(pdfPath));
        // 創(chuàng)建PdfDocument實(shí)例
        PdfDocument pdf = new PdfDocument(writer);
        // 設(shè)置頁(yè)面大小
        pdf.setDefaultPageSize(PageSize.A4);
        // 創(chuàng)建Document實(shí)例
        Document document = new Document(pdf);
        // 轉(zhuǎn)換HTML到PDF
        HtmlConverter.convertToPdf(new FileInputStream(htmlPath), pdf);
        // 關(guān)閉document
        document.close();
    }
}

在上述代碼中,我們首先創(chuàng)建了一個(gè)PdfWriter實(shí)例,然后創(chuàng)建了一個(gè)PdfDocument實(shí)例,并設(shè)置了頁(yè)面大小,我們創(chuàng)建了一個(gè)Document實(shí)例,并使用HtmlConverter將HTML轉(zhuǎn)換為PDF,我們關(guān)閉了Document實(shí)例。

這只完成了HTML到PDF的基本轉(zhuǎn)換,如果我們的HTML中包含圖片,并且我們希望這些圖片在PDF中有正確的寬高,我們需要進(jìn)行一些額外的處理,我們可以使用iText7的Image類來(lái)處理圖片,以下是一個(gè)示例,展示了如何在轉(zhuǎn)換HTML到PDF時(shí)設(shè)置圖片的寬高:

import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.property.UnitValue;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import javax.xml.parsers.*;
import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
public class HtmlToPdf {
    public static void main(String[] args) throws Exception {
        String htmlPath = "path/to/your/html/file";
        String pdfPath = "path/to/your/pdf/file";
        convertHtmlToPdfWithImageSize(htmlPath, pdfPath, "100%", "100%");
    }
    public static void convertHtmlToPdfWithImageSize(String htmlPath, String pdfPath, String width, String height) throws Exception {
        // Create a list to store image informations (width and height) from the HTML file
        Map imagesInfos = new HashMap<>();
        // Get the factory object for creating XML factories
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // Get the actual builder instance for parse HTML content to XML content by using the factory instance created above and specifying the namespace Aware feature to "false" to avoid any parsing issues with unrecognized namespaces in the HTML content as it is not XHTML compliant HTML content (it can contain custom tags or attributes that are not part of the standard HTML specification) and set the error handler to null to suppress all error messages and warnings during the parsing process as we don't need them for this example purposes only to extract image informations from the HTML content and finally create an instance of the builder class by calling its newDocumentBuilder method passing false as the second argument to specify that we don't want to use DTD validation while parsing the HTML content which can be time consuming if the HTML content is large or contains many elements with complex structures and attributes but also can lead to parsing errors if there are any missing or invalid DTD declarations in the HTML content or if the HTML content is not wellformed or valid according to the specified DTD schema but in our case we know that the HTML content is wellformed and valid and doesn't contain any custom tags or attributes that are not part of the standard HTML specification so we don't need to worry about any parsing issues or errors related to DTD validation or namespace awareness as we will parse it using a simple and straightforward way that should work fine for most cases without any issues or problems even if the HTML content is quite large or complex as long as it follows some basic rules like having proper opening and closing tags for each element, using correct attribute syntax, etc... Also note that we will ignore all whitespace characters including newlines, tabs, spaces, etc... as they don't affect the meaning or structure of the HTML content and can be safely removed without changing anything else except making the HTML content cleaner and easier to read and understand by humans but not affecting its parsing behavior or results in any way... Finally, parse the HTML content into an instance of org.w3c

標(biāo)題名稱:itext7html轉(zhuǎn)pdf圖片寬高
路徑分享:http://www.5511xx.com/article/ccoisjj.html