บทความนี้จะแสดงวิธีการสร้าง PDF จาก HTML ด้วย Java คุณสามารถจัดการและแสดงผลเอกสาร HTML รวมถึงสไตล์ CSS ได้อย่างง่ายดาย.
Aspose.HTML for Java
ช่วยให้แอปพลิเคชันของคุณทำงานด้านการจัดการ HTML ได้หลากหลายและรองรับคุณลักษณะ HTML5, CSS3, SVG และ HTML Canvas ด้วย API ของเรา คุณสามารถโหลดเว็บเพจ, วิเคราะห์, แก้ไขเนื้อหา, และบันทึกหรือแปลงหน้าได้. รูปแบบไฟล์ที่คุณสามารถจัดการได้รวมถึง: XHTML, MHTML, SVG, MD, PDF, XPS, EPUB, PNG, TIFF, JPEG, BMP และอื่น ๆ.
PDF จาก HTML ใน Java
ชุดคุณลักษณะที่หลากหลาย
Aspose.HTML for Java ให้คุณหลายคุณลักษณะในหลายด้าน:
• Document object model - อนุญาตให้คุณแก้ไข, สร้างและลบโหนด, และสร้างขึ้นตามเอกสาร HTML อย่างเป็นทางการ.
• ตัวแปลงความเร็วสูง. สำหรับข้อมูลเพิ่มเติมเกี่ยวกับรูปแบบไฟล์ที่รองรับ โปรดเยี่ยมชมบทความ รายการคุณลักษณะ
หรือ การแปลง
.
• การประมวลผล CSS, JavaScript, และรูปแบบอื่น ๆ ที่มีในตัว.
สร้าง PDF จาก HTML ด้วยบรรทัดโค้ดเดียวของ Java
คุณสามารถสร้าง PDF จาก HTML ในแอปพลิเคชัน Java ของคุณได้ด้วยบรรทัดโค้ดเดียวโดยใช้เมธอดสถิตของคลาส Converter
// 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"
);
สร้าง PDF จาก HTML ทีละบรรทัดด้วย Java
ด้านล่างเป็นขั้นตอนการสร้าง PDF จาก HTML ทีละบรรทัด:
- ใช้คลาส HtmlDocument
เพื่อโหลดไฟล์ HTML ก่อน.
- หลังจากนั้นสร้างอินสแตนซ์ของ PdfSaveOptions
.
- ด้วยเมธอด ConvertHTML()
ของคลาส Converter
บันทึก HTML เป็นไฟล์ PDF คุณต้องส่งผ่าน HTMLDocument
, PdfSaveOptions
และเส้นทางไฟล์ผลลัพธ์ไปยังเมธอด ConvertHTML() เพื่อสร้าง PDF จาก HTML.
// 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();
}
}
ปรับแต่งกระบวนการเรนเดอร์ด้วยตัวเลือกการบันทึก
คุณสามารถปรับแต่งกระบวนการเรนเดอร์ด้วย PdfSaveOptions
, และระบุ ขนาดหน้า
, สิทธิ์ไฟล์
, ขอบกระดาษ
, ประเภทสื่อ CSS
และอื่น ๆ อีกมาก ในตัวอย่างด้านล่างคุณจะเห็นวิธีใช้ PdfSaveOptions เพื่อสร้างไฟล์ PDF ด้วยขนาดหน้าที่กำหนดเองและสีพื้นหลัง:
// 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"
);
ในบทความ การปรับแต่งตัวแปลง
คุณสามารถค้นหาเพิ่มเติมเกี่ยวกับ PdfSaveOptions
.
ตัวจัดหาสตรีมผลลัพธ์
คุณสามารถนำอินเทอร์เฟซ MemoryStreamProvider
ไปใช้ได้หากต้องการบันทึกไฟล์ในที่เก็บระยะไกล เช่น คลาวด์หรือฐานข้อมูล MemoryStreamProvider ถูกออกแบบเป็นอ็อบเจกต์ callback เพื่อสร้างสตรีมและปล่อยสตรีมที่สร้างไว้ล่วงหน้าหลังจากเรนเดอร์หน้าหรือเอกสารเสร็จ.
เราจะนำเสนอ ตัวอย่างการใช้ MemoryStreamProvider ด้านล่าง:
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();
}
}
}
Aspose ให้การสนับสนุนด้านเทคนิคสำหรับผลิตภัณฑ์ทั้งหมดของตน ตั้งแต่ บทความฐานความรู้
, เอกสาร
, บล็อก
, ตัวอย่างโค้ด
, และ สาธิต
, ไปจนถึง การสนับสนุนฟรี
รวมถึง การสนับสนุนแบบชำระเงิน
ที่พร้อมให้บริการ. หากคุณต้องการผู้เชี่ยวชาญที่ทำงานร่วมกับคุณและช่วยคุณนำ Aspose.HTML for Java ไปใช้ตามความต้องการของคุณ โซลูชันที่ดีที่สุดคือ ที่ปรึกษาแบบชำระเงิน
.