I det här exemplet ska vi se hur man konverterar HTML till PDF med C++. HTML till PDF-konvertering är ofta nödvändig funktion och du kan enkelt uppnå detta genom att skriva några rader kod i C++.
Steg för att konvertera HTML till PDF med C++
- Använd NuGet-paketet Aspose.Words.Cpp
- Inkludera hänvisning till Aspose::Words och Spara namnområden
- Skapa en ny Document Class-instans
- Spara HTMl till PDF i C++ med hjälp av Spara-metoden
I följande exempel kan du enkelt rendera HTML till PDF med C++ utan några externa beroenden och med några enkla steg och kod.
Kod för att konvertera HTML till PDF med 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); | |
} | |
}; |
I tidigare exempel lärde vi oss Hur man skapar Word-dokument i C++. Nu har vi sett hur HTML till PDF renderas i C++. Det här exemplet gör det mycket enklare och snabbare att skapa word-dokument utan att vara beroende av Microsoft-plattformen.