Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Feb 26, 2025 12:41 pm

Hi,

I have an application which I want to host on GCP and generate pdf from html. Can you please help how this can be fixed

I have tried multiple options, both of them doesn't download the file

when i try memory stream the stream length is 0

when I try with to create a pdf on tmp in gcp the file doesn't get created
But I see in this in gcp logs.

OutPdfFileName:/tmp/192a08c5-fdfe-417f-a680-5e3b2434342b.pdf
PageSize:(1000,1000)
PageMargins:(0,0,0,0)
Start conversion...
Conversion finished.

Option 1:

HtmlConverter.PluginPath = pluginPath;
using (MemoryStream stream = new MemoryStream())
{
HtmlConverter.Convert(model.Content, stream, true, int.MaxValue, new SizeF(model.width, model.height), new PdfMargins(0), LoadHtmlType.URL);
stream.Position = 0;
Console.WriteLine("stream.Length.ToString()" + stream.Length.ToString());

return File(stream.ToArray(), "application/pdf", model.FileName);
}

Option 2:

HtmlConverter.Convert(model.Content, filePathPdf, true, int.MaxValue, new SizeF(model.width, model.height), new PdfMargins(0), LoadHtmlType.SourceCode);

byte[] fileBytes = await System.IO.File.ReadAllBytesAsync(model.FileName);

var memoryStream = new MemoryStream(fileBytes);
return File(memoryStream.ToArray(), "application/pdf", model.FileName);



Thanks
Anoop

amacharla
 
Posts: 3
Joined: Mon Aug 07, 2023 3:26 pm

Thu Feb 27, 2025 3:16 am

Dear Anoop,

Thanks for your inquiry.
In order for us to better investigate your issue and provide corresponding solutions, please provide the following detailed information.
1. Your project type, such as Console App, NET8
2. The version of Spire.PDF you are using
3. What method do you use to deploy to GCP, and is it Virtual Machine Service (Compute Engine)? Or is it a serverless platform (APP Engine)?
4. Your input HTML file

Thanks in advance for your assistance.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1379
Joined: Tue Sep 27, 2016 1:06 am

Thu Feb 27, 2025 4:31 am

The project type is .Net Core 8.0 API and "Spire.PDF" Version="11.2.4" and its a serverless App engine runs in linux.
Any basic html to didn't work.
And i am using the plugin as well.

amacharla
 
Posts: 3
Joined: Mon Aug 07, 2023 3:26 pm

Thu Feb 27, 2025 10:18 am

Hi,

Thanks for your feedback.
The possible reason for the inability to generate result PDF is that QT is a cross platform GUI library that typically requires graphical interface support. For your serverless environment, please run the program through xvfb-run dotnet YourWebApi.dll.

Besides, the QT has a high dependency on the environment. Our Spire.PDF also provides a new method to convert HTML to PDF using chrome.exe, eliminating the need for QT plugin. The new method requires you to install Google Chrome on your system. As far as I know, for a serverless App engine, this is a hosted environment and Google Chrome cannot be installed. I suggest you deploy the project in a Docker container. If you accept this approach, I will send you detailed steps.
Convert HTML to PDF using ChromeHtmlConverter

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1379
Joined: Tue Sep 27, 2016 1:06 am

Thu Feb 27, 2025 5:06 pm

Yes, if you can send the solution that would be great, I can try that let you know if that worked.

amacharla
 
Posts: 3
Joined: Mon Aug 07, 2023 3:26 pm

Fri Feb 28, 2025 3:15 am

Dear Anoop,

The following are the steps to use ChromeHtmlConverter class to do the conversion in docker container:
1. Dockerfile
Code: Select all
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base

# Set environment variables
ENV DEBIAN_FRONTEND noninteractive
ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn

# Install Google Chrome and its necessary dependencies
RUN apt-get update && apt-get install software-properties-common apt-transport-https ca-certificates curl -y
RUN curl -fSsL https://dl.google.com/linux/linux_signing_key.pub | gpg --dearmor | tee /usr/share/keyrings/google-chrome.gpg >> /dev/null
RUN echo deb [arch=amd64 signed-by=/usr/share/keyrings/google-chrome.gpg] http://dl.google.com/linux/chrome/deb/ stable main | tee /etc/apt/sources.list.d/google-chrome.list
RUN apt-get update && apt-get install google-chrome-stable -y

# Install ChromeDriver
RUN apt-get update && \
    apt-get install -y unzip && \
    wget https://chromedriver.storage.googleapis.com/2.31/chromedriver_linux64.zip && \
    unzip chromedriver_linux64.zip && \
    mv chromedriver /usr/bin && rm -f chromedriver_linux64.zip

#Set working directory
WORKDIR /app

2. Build a NET8 image based on Dockerfile and install Google at the same time (When installing Google, it may take a long time. It is recommended to configure Docker image accelerator first and then build the image)
Code: Select all
docker build -t ninaimage .

3. Check the built image
Code: Select all
docker images

4. Create container based on the built image
Code: Select all
docker run -it -- name ninaapp ninaimage /bin/bash

5. After creating the container, it will automatically enter the interior of the container and use the command to search for the Google version and the path of the Google executable file
Code: Select all
google-chrome --version

which google-chrome

6. Change the path of the Google executable file in your project. Please refer to the article provided to you yesterday for the complete code: Convert HTML to PDF using ChromeHtmlConverter
Code: Select all
string chromeLocation = "/usr/bin/google-chrome";
ChromeHtmlConverter converter = new ChromeHtmlConverter(chromeLocation);

7. Publish your project to the linux-x64 folder in the Visual Studio tool

8. Copy the published linux-x64 folder to the docker container
Code: Select all
docker cp linux-x64 <your-container-id>:/app/

9. Enter the container to execute the project
Code: Select all
docker exec -it <your-container-id> /bin/bash

cd linux-x64/

dotnet YourPorjectName.dll

10. Exit the container and copy the generated PDF to the host computer
Code: Select all
exit

docker cp <your-container-id>:/app/linux-x64/urlToPDF.pdf /root/nina/

Please follow my steps to conduct the test on your end, and if there are any issues, please feel free to provide feedback at any time.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1379
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.PDF

cron