DocumentService is a library with the following functions
- Generate Word documents - Read Word document files as a template and replace the placeholder with actual data.
- Generate PDF documents - Read an HTML file as a template and replace placeholders with actual data. Convert the HTML file to PDF
- Replace placeholders in text paragraph with values.
- Replace placeholders in tables.
- Multiple placeholders in the same table cell/line/paragraph can be replaced.
- Populate table with new data.
- Replace images with image's placeholder. The image's position will be maintained based on the position of its placeholder image. Image size will also be maintained based on placeholder image.
- Converts an HTML document to PDF.
- Replace placeholders in the document with actual string data.
Setting up the app in a Docker-based environment enables developers of non-Windows origins to run the backend application on their machine to test the APIs.
- Install Docker on your machine. Choose to follow the instructions based on your device OS.
- Install Docker Compose. A separate installation is required for Linux-based OS. If you are using Windows or macOS, installing the Docker Desktop app includes Docker Compose.
- Clone the project
document-service
. - (Optional) Install Docker Extension for VS Code.
- In the root directory of the project, create a new file
.env
. - Copy data from example template into
.env
. Then set suitable JWT key. - Set
environment
variablesASPNETCORE_ENVIRONMENT
andBUILD_CONFIGURATION
as per requirement in docker-compose.yaml. Ensure correct formatting:
- ASPNETCORE_ENVIRONMENT=Development
- BUILD_CONFIGURATION=Debug
- ASPNETCORE_ENVIRONMENT=Development
- BUILD_CONFIGURATION=Release
- ASPNETCORE_ENVIRONMENT=Production
- BUILD_CONFIGURATION=Release
- Ensure Docker is running.
- Then, in the root directory of the project, execute the following command to build a container for
document-service
:
docker-compose -f docker-compose.yaml up
- The project will run on
http://localhost:5000
. Please check Troubleshooting if the build failed. - You can access the Swagger UI at
http://localhost:5000/swagger/index.html
in Development Environment. - Test the API via Postman. The app can be accessed using
http://localhost:5000/<API>
.
A known issue while building the container is the following:
E: failed to solve: process "/bin/sh -c <sample Dockerfile step>" did not complete successfully: exit code: 100
This is a network related issue where it is failing to fetch files from an external source. It can be verified in the Docker logs:
E: Failed to fetch http://sample/link/for.file Unable to connect to sample.download.location:80: [IP: ...]
Solution: Prune the failed build and rebuild the application using the following commands:
# prune all unused containers, networks, images, build cache
docker system prune -a
# rebuild the container
docker-compose -f docker-compose.yaml up
NOTE: Please go through the official documentation on prune command before using it.
- Go to website: https://wkhtmltopdf.org/downloads.html
- Select the version of wkhtmltopdf installer that you need to download based on your system requirements.
- Finish Installation.
- Go to the location to the bin files of your project where the DocumentService DLL is located.
- Create a folder called Tools and place the wkhtmltopdf.exe file there. wkhtmltopdf.exe can be found in the Program Files in C directory after it is installed.
Note: We use a Temp folder to temporarily hold the modified HTML file before converting it to a PDF file. After the conversion is done, the temporary file is removed. The code is already provided with the location of the temp file, so no modification is required in the code, and the temp folder will be used automatically.
List<ContentMetaData> contentList = new List<ContentMetaData>
{
new ContentMetaData { Placeholder = "Incident UID", Content = "I-20230822-001" },
new ContentMetaData { Placeholder = "Description", Content = "Suspicious activity reported" },
new ContentMetaData { Placeholder = "Site", Content = "Headquarters" }
};
// Tools\\index.html - The path of the html template file in which changes are to be made.
// Tools\\OutputFile.pdf - The path of the final pdf output file.
PdfDocumentGenerator.GeneratePdfByTemplate("Tools\\index.html", contentList, "Tools\\OutputFile.pdf");
string templateFilePath = @"C:\Users\Admin\Desktop\Osmosys\Work\Projects\Document Service Component\Testing\Document.docx";
string outputFilePath = @"C:\Users\Admin\Desktop\Osmosys\Work\Projects\Document Service Component\Testing\Test_Output.docx";
List<TableData> tablesData = new List<TableData>()
{
new TableData()
{
TablePos = 5,
Data = new List<Dictionary<string, string>>()
{
new Dictionary<string, string>()
{
{ "Item Name", "1st med" },
{ "Dosage", "1" },
{ "Quantity", "1" },
{ "Precautions", "Take care" }
},
new Dictionary<string, string>()
{
{ "Item Name", "2nd med" },
{ "Dosage", "1" },
{ "Quantity", "1" },
}
}
}
};
List<ContentData> contents = new List<ContentData>()
{
new ContentData
{
Placeholder = "Picture 1",
Content = @"../testImage1.jpg",
ContentType = ContentType.Image,
ParentBody = ParentBody.None
},
new ContentData
{
Placeholder = "Picture 2",
Content = @"../testImage2.jpg",
ContentType = ContentType.Image,
ParentBody = ParentBody.None
},
};
DocumentData documentData = new DocumentData()
{
Placeholders = contents,
TablesData = tablesData
};
WordDocumentGenerator.GenerateDocumentByTemplate(templateFilePath, documentData, outputFilePath);
- .NET Framework 4.5.2
- .NET Standard 2.0 - Can be installed as a dependency in applications running on .NET Framework 4.8 and modern .NET (Core, v5, v6 and later).
The DocumentService is licensed under the MIT license.
We appreciate the time and effort put in by all contributors to make this project better!