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.

Thu Nov 09, 2023 3:16 am

1. We are getting 500 inter server error error code while converting large HTML to PDF

2.
Code: Select all
public static String convertHtmlToPdf(String htmlString) throws IOException {

      Path tempFile = Files.createTempFile("output", ".html");

      try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(tempFile.toFile().getPath()),
            StandardCharsets.UTF_8)) {
         writer.append(htmlString);
         writer.newLine();
      } catch (IOException e) {
         throw e;
      }

      logger.info("temp file created: " + tempFile.toFile().getPath());

      File outputFile = File
            .createTempFile("output_pdf_" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()), ".pdf");

      // Set license key
      com.spire.license.LicenseProvider.setLicenseKey(LICENSE_KEY);

      // Set plugin path
      HtmlConverter.setPluginPath(PLUGIN_PATH);

      // Convert HTML string to PDF
      HtmlConverter.convert(tempFile.toFile().getPath().toString(), new FileOutputStream(outputFile), true,
            1000000000, new Size(700, 900), new PdfMargins(40, 40, 40, 40));
      logger.info("outputResource.getFile().length() " + outputFile.length());

      return addPageNumberToPDF(outputFile.getAbsolutePath());

   }





Manifest-Version: 1.0
Extension-Name: spire.office
Implementation-Title: spire.office for java
Implementation-Version: 8.10.2
Implementation-Vendor: E-iceblue Co., Ltd.
Implementation-Vendor-Id: com.spire
Implementation-URL: https://www.e-iceblue.com

4. Application Type: Spingboot JDK 1.8

Dasgupta
 
Posts: 54
Joined: Fri Mar 11, 2022 9:14 am

Thu Nov 09, 2023 9:40 am

Hi,

Thanks for your feedback.
I have investigated the error log you provided, and after testing it with the HTML from the log, I was unable to reproduce the problem.

To assist you further, could you please provide the following information:

1). HTML file.
2). Test environment, such as Windows 10 64 bit.
3). The version of Qt Plugin.

By providing these additional details, we can investigate the issue more effectively and offer you the appropriate solution. Should you have any questions or require further assistance, please feel free to reach out to me.

I appreciate your cooperation and look forward to resolving this issue for you swiftly.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Tue Nov 14, 2023 7:44 am

Hello,

Please find requested details below.

1. Attached.
2. OS Name: Microsoft Windows Server 2019 Standard
OS Version: 10.0.17763 N/A Build 17763
System Type: x64-based PC

Dasgupta
 
Posts: 54
Joined: Fri Mar 11, 2022 9:14 am

Tue Nov 14, 2023 10:21 am

Hi,

Thanks for your feedback.

have created a Springboot project and performed testing. I found that the HTML you provided was successfully converted to PDF.
result.png

However, I would like to confirm with you if only this HTML document is unable to be converted while all other HTML documents are able to be converted?

Additionally, I would like to kindly request that you check the folder permissions of the Qt plugin folder. Please also test the conversion process on your local computer to see if this issue only occurs in your Windows server environment.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Wed Nov 15, 2023 10:37 am

Hello,

Can you tell me where i will find Qt plugin folde.

Dasgupta
 
Posts: 54
Joined: Fri Mar 11, 2022 9:14 am

Thu Nov 16, 2023 6:37 am

Hi,

Thank you for your feedback. I would like to clarify the location of the Qt plugin folder, as mentioned in our previous conversation. In the context of your code example: HtmlConverter.setPluginPath(PLUGIN_PATH), PLUGIN_PATH refers to the directory where the plugins are stored.

To ensure proper functionality, please make sure that the plugin folder has sufficient permissions, ideally granting read, write, and execute access.

If you have any further questions or need additional assistance, please feel free to reach out. We are here to help.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Mon Nov 20, 2023 4:14 pm

Hello,

I have checked and Qt plugin folder has all the permission.
We are getting 400 bad request error for attached HTML.

Code: Select all
/**
    * @apiNote This API us used to convert HTML to PDF
    *
    * @param htmlString
    * @param request
    * @return
    * @throws UnsupportedEncodingException
    */
   @PostMapping("/html-to-pdf")
   public ResponseEntity<Resource> htmltopdf(@RequestParam("htmlString") String htmlString,
         @RequestParam("templateName") String templateName, HttpServletRequest request)
         throws UnsupportedEncodingException {
      logger.info("inside uploadHTMLString");
      logger.info("htmlString " + htmlString);
      logger.info("templateName " + templateName);
      htmlString = htmlString.replace("%", "%25");
      htmlString = decode(htmlString);
      logger.info("decoded htmlString " + htmlString);


      String htmlContent = "";
      String template = "";
      if (templateName != null && !templateName.isEmpty()) {
         try {
            Resource resource = resourceLoader.getResource("classpath:/pdf-template/" + templateName + ".html");
            template = Resources.toString(resource.getURL(), StandardCharsets.UTF_8);
            htmlContent = template.replace(HTML_CONTENT, htmlString);
         } catch (IOException e) {
            logger.error(e.getMessage());
         }
      } else {
         htmlContent = htmlString;
      }

      Resource outputResource = null;
      try {
         outputResource = fileStorageService.loadFileAsResource(convertHtmlToPdf(htmlContent));
      } catch (Exception e) {
         logger.error(e.toString());
      }

      return ResponseEntity.ok().contentType(MediaType.parseMediaType("application/pdf"))
            .header(HttpHeaders.CONTENT_DISPOSITION,
                  "attachment; filename=\"" + outputResource.getFilename() + "\"")
            .body(outputResource);
   }

public static String convertHtmlToPdf(String htmlString) throws IOException {

      Path tempFile = Files.createTempFile("output", ".html");

      try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(tempFile.toFile().getPath()),
            StandardCharsets.UTF_8)) {
         writer.append(htmlString);
         writer.newLine();
      } catch (IOException e) {
         throw e;
      }

      logger.info("temp file created: " + tempFile.toFile().getPath());

      File outputFile = File
            .createTempFile("output_pdf_" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()), ".pdf");

      // Set license key
      com.spire.license.LicenseProvider.setLicenseKey(LICENSE_KEY);

      // Set plugin path
      HtmlConverter.setPluginPath(PLUGIN_PATH);

      // Convert HTML string to PDF
      HtmlConverter.convert(tempFile.toFile().getPath().toString(), new FileOutputStream(outputFile), true,
            1000000000, new Size(700, 900), new PdfMargins(40, 40, 40, 40));
      logger.info("outputResource.getFile().length() " + outputFile.length());

      return addPageNumberToPDF(outputFile.getAbsolutePath());

   }

private static String addPageNumberToPDF(String fileName) throws IOException {

      // Create a PdfDocument instance
      PdfDocument pdf = new PdfDocument();
      // Load the PDF document
      pdf.loadFromFile(fileName);

      // Create a PdfTrueTypeFont instance
      PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 10));

      // Create a PdfPageNumberField instance
      PdfPageNumberField pageNumberField = new PdfPageNumberField(font, PdfBrushes.getBlack());

      // Create a PdfPageCountField instance
      PdfPageCountField pageCountField = new PdfPageCountField(font, PdfBrushes.getBlack());

      // Create a PdfCompositeField instance, add the page number filed and the page
      // count field to composite filed
      PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.getBlack(), "Page {0} of {1}",
            pageNumberField, pageCountField);

      // Set string format for the composite field
      compositeField.setStringFormat(new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Top));

      // Loop through the pages
      for (int i = 0; i < pdf.getPages().getCount(); i++) {
         PdfPageBase page = pdf.getPages().get(i);
         float x = (float) page.getSize().getWidth() / 2 - 20;
         float y = (float) page.getSize().getHeight() - pdf.getPageSettings().getMargins().getBottom();
         // Draw the composite filed on each page
         compositeField.draw(page.getCanvas(), x, y);
      }

      // Save the result document
      pdf.saveToFile(fileName);

      return fileName;
   }

private String decode(String value) throws UnsupportedEncodingException {
      return URLDecoder.decode(value, StandardCharsets.UTF_8.toString());
   }

Thanks and regards,
Umesh Asodekar

Dasgupta
 
Posts: 54
Joined: Fri Mar 11, 2022 9:14 am

Wed Nov 22, 2023 7:13 am

Hi,

Thank you for reaching out to us regarding the issue you encountered while using our product to convert HTML to PDF. We apologize for the inconvenience caused.

After investigating the matter, we have determined that the problem is related to a request error rather than an error during the conversion process itself. We have tested the HTML provided by you on a Windows Server 2016 environment, and it successfully converted without any issues.

To further assist you, we kindly request that you check your request method to ensure it aligns with the example shown in the attached image.
request.png

If the problem persists, please provide us with the error logs or any additional information you may have regarding the error message.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.PDF