Dalam contoh ini, kita akan melihat cara mengonversi HTML ke PDF menggunakan C++. Konversi HTML ke PDF sering kali merupakan fitur yang diperlukan dan Anda dapat dengan mudah mencapainya dengan menulis beberapa baris kode dalam C++.
Langkah-langkah untuk Mengkonversi HTML ke PDF menggunakan C++
- Gunakan Aspose.Words.Cpp paket NuGet
- Sertakan referensi ke Aspose::Words dan Menyimpan ruang nama
- Buat instance Document Class baru
- Simpan HTML ke PDF dalam C++ dengan menggunakan metode Simpan
Dalam contoh berikut, Anda dapat dengan mudah merender HTML ke PDF menggunakan C++ tanpa ketergantungan eksternal dan dengan beberapa langkah dan kode sederhana.
Kode untuk Mengonversi HTML ke PDF menggunakan C++
#include <Aspose.Words.Cpp/Document.h> | |
#include <Aspose.Words.Cpp/DocumentBuilder.h> | |
#include <Aspose.Words.Cpp/Saving/DocSaveOptions.h> | |
#include <Aspose.Words.Cpp/Saving/SaveOutputParameters.h> | |
#include <system/io/file.h> | |
#include <Aspose.Words.Cpp/License.h> | |
using System::ArrayPtr; | |
using System::MakeArray; | |
using System::MakeObject; | |
using System::SharedPtr; | |
using System::String; | |
using namespace Aspose::Words; | |
using namespace Aspose::Words::Saving; | |
class HtmlToPdfConvertor | |
{ | |
public: | |
void HtmlToPdfConversion() | |
{ | |
// File name and path of license file | |
System::String testLicenseFileName = u"Aspose.Total.NET.lic"; | |
// Setting the Aspose.Words before creating Word document | |
SharedPtr<License> wordsLicense = System::MakeObject<License>(); | |
// Setting license | |
wordsLicense->SetLicense(testLicenseFileName); | |
// Create an instance of Document class of Aspose.Words for C++ | |
// to load the HTML file | |
SharedPtr<Document> WordDocumentUsingCPP = MakeObject<Document>(u"HtmlFile.html"); | |
// Save HTML to PDF in C++ using Save method | |
WordDocumentUsingCPP->Save(u"Html_To_PDF_using_CPP.pdf", SaveFormat::Pdf); | |
} | |
}; |
Pada contoh sebelumnya, kita mempelajari Cara Membuat Dokumen Word di C++. Sekarang, kita telah melihat bagaimana di C++, HTML ke PDF dirender. Contoh ini membuat pembuatan dokumen word jauh lebih mudah dan cepat tanpa ketergantungan pada platform Microsoft.