Bunachar Eolais

Gin PDF ó HTML ag baint úsáide as Java [Céim ar chéim]

Léiríonn an t-alt seo conas PDFanna a ghiniúint ó HTML ag baint úsáide as Java. Is féidir leat doiciméid HTML a láimhseáil agus a rindreáil go héasca, lena n-áirítear stíleanna CSS.

Aspose.HTML do Java cuireann ar do chumas feidhmeanna láimhseála HTML i leith leathan a dhéanamh agus tacaíonn sé le HTML5, CSS3, SVG, agus gnéithe HTML Canvas. Leis an API againn is féidir leat leathanaigh ghréasáin a luchtú, an t-ábhar a anailísiú, a chur in eagar, agus iad a shábháil agus a thiontú.
Formáidí coitianta is féidir leat láimhseáil: XHTML, MHTML, SVG, MD, PDF, XPS, EPUB, PNG, TIFF, JPEG, BMP, agus eile.

PDF ó HTML i Java

Sraith Saibhir de Ghnéithe

Aspose.HTML do Java cuireann raon leathan gnúis ar fáil i go leor réimsí éagsúla:

• Samhail réadaigh doiciméid – ligeann sé duit nóid a chur in eagar, a chruthú agus a bhaint, agus tá sé tógtha de réir an doiciméid oifigiúil HTML.
• Tiontairí ard-úrram. Le haghaidh tuilleadh eolais faoi fhormáidí comhaid a dtacaítear leo, tabhair cuairt ar Liosta na Gnéithe nó ar altanna Áthchur .
• Próiseáil CSS, JavaScript, agus formáidí eile ionsuite.

Gin PDF ó HTML le líne amháin de chód Java

Is féidir leat PDF ó HTML a ghiniúint go héasca i d’iarratas Java le litir amháin de chód ag baint úsáide as na modhanna statach de rang an Conraitheoir .

// Invoke the ConvertHTML method to convert the HTML to PDF.
com.aspose.html.converters.Converter.convertHTML(
        "<span>Hello World!!</span>",
        ".",
        new com.aspose.html.saving.PdfSaveOptions(),
        "output.pdf"
);

Gin PDF ó HTML líne ar líne ag baint úsáide as Java

Thíos na céimeanna chun PDF ó HTML a ghiniúint líne ar líne:

  1. Ag baint úsáide as rang HtmlDocument lódáil an comhad HTML ar dtús.
  2. Ansin cruthaigh sampla de PdfSaveOptions .
  3. Le modh ConvertHTML() den rang Conraitheoir sábháil an HTML mar chomhad PDF. Beidh ort an HTMLDocument , an PdfSaveOptions , agus conair an chomhaid aschur a chur ar an modh ConvertHTML() chun PDF ó HTML a ghiniúint.
// Prepare an HTML code and save it to the file.
String code = "<span>Hello World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
    fileWriter.write(code);
}

// Initialize an HTML document from the file
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("document.html");
try {
    // Initialize PdfSaveOptions
    com.aspose.html.saving.PdfSaveOptions options = new com.aspose.html.saving.PdfSaveOptions();

    // Convert HTML to PDF
    com.aspose.html.converters.Converter.convertHTML(
            document,
            options,
            "output.pdf"
    );
} finally {
    if (document != null) {
        document.dispose();
    }
}

Saincheap an próiseas rindreála leis an rogha Sábháil

Is féidir leat an próiseas rindreála a shaincheapadh le PdfSaveOptions , agus an méid an leathanaigh , ceadanna comhaid , imeall , cineál meán CSS , agus go leor eile a shonrú. Sa shampla thíos, feictear conas PdfSaveOptions a úsáid chun comhad PDF a chruthú le méid leathanaigh saincheaptha agus dath cúlra glas:

// Prepare an HTML code and save it to the file
String code = "<span>Hello</span> <span>World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
    fileWriter.write(code);
}

// Set A5 as a page-size and change the background color to green
com.aspose.html.saving.PdfSaveOptions options = new com.aspose.html.saving.PdfSaveOptions();
com.aspose.html.rendering.PageSetup pageSetup = new com.aspose.html.rendering.PageSetup();
com.aspose.html.drawing.Page anyPage = new com.aspose.html.drawing.Page();
anyPage.setSize(
        new com.aspose.html.drawing.Size(
                com.aspose.html.drawing.Length.fromInches(8.3f),
                com.aspose.html.drawing.Length.fromInches(5.8f)
        )
);
pageSetup.setAnyPage(anyPage);
options.setPageSetup(pageSetup);

options.setBackgroundColor(com.aspose.html.drawing.Color.getGreen());

// Convert HTML document to PDF
com.aspose.html.converters.Converter.convertHTML(
        "document.html",
        options,
        "output.pdf"
);

Sa alt Coigeartú na gConraitheoirí , is féidir leat tuilleadh eolais a fháil faoi PdfSaveOptions .

Soláthraí sruth aschur

Is féidir leat comhéadan MemoryStreamProvider a chur i bhfeidhm má theastaíonn uait comhaid a shábháil i stóras cianda, mar shampla sa scamaill nó i mbunachar sonraí.
Tá comhéadan MemoryStreamProvider deartha mar réad glaoch ar ais chun sruth a chruthú agus an sruth cruthaithe roimh ré a scaoileadh tar éis an leathanaigh nó an doiciméid a rindreáil.

Taispeánfaimid sampla de úsáid MemoryStreamProvider thíos:

package com.aspose.html.examples.java;

// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-Java
public class MemoryStreamProvider implements java.io.Closeable {
    // List of InputStream objects created during the document rendering
    public java.util.List<java.io.InputStream> lStream = new java.util.ArrayList<>();

    @Override
    public void close() throws java.io.IOException {
        for (java.io.InputStream stream : lStream) {
            stream.close();
        }
    }
}
// Create an instance of MemoryStreamProvider
try (MemoryStreamProvider streamProvider = new MemoryStreamProvider()) {
    // Initialize an HTML document
    com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("<span>Hello World!!</span>", ".");
    try {
        // Convert HTML to PDF by using the MemoryStreamProvider
        com.aspose.html.converters.Converter.convertHTML(
                document,
                new com.aspose.html.saving.PdfSaveOptions(),
                streamProvider.lStream
        );

        // Get access to the memory stream that contains the result data
        java.io.InputStream inputStream = streamProvider.lStream.stream().findFirst().get();

        // Flush the result data to the output file
        try (java.io.FileOutputStream fileOutputStream = new java.io.FileOutputStream("output.pdf")) {
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            fileOutputStream.write(buffer);
        }
    } finally {
        if (document != null) {
            document.dispose();
        }
    }
}

Soláthraíonn Aspose tacaíocht theicniúil do gach ceann dá tháirgí, ó aiteanna bhunachar eolais , doiciméadú , blag , samplaí cód , agus taispeántais , go dtí Tacaíocht Saor chomh maith le Tacaíocht Íoctha .
Má theastaíonn saineolaithe a oibríonn leatsa agus cabhróidh siad leat Aspose.HTML do Java a chur i bhfeidhm de réir do riachtanas, is é an réiteach is fearr ná ár Comhairle Íoctha .