0

Previously I was using http://do.convertapi.com/Web2Pdf to convert HTML String to PDF using a simple GET request (not POST) using Java. The entire content was passed using curl parameter.

However, that API seems to have stopped working recently. I'm trying to port over to https://v2.convertapi.com/web/to/pdf but I cannot find a sample to do the same using the new API either with GET or POST.

Can someone provide an example to make a GET or POST request using Java?

UPDATE: I have managed to make it work.

private static final String WEB2PDF_API_URL = "https://v2.convertapi.com/html/to/pdf";
private static final String WEB2PDF_SECRET = "secret-here";

String htmlContent = "valid HTML content here";

URL apiUrl = new URL(WEB2PDF_API_URL + "?secret=" + WEB2PDF_SECRET + "&download= attachment&PageOrientation=landscape&MarginLeft=0&MarginRight=0&MarginTop=0&MarginBottom=0");
HttpURLConnection connection = null;

ByteArrayOutputStream buffer = null;

connection = (HttpURLConnection) apiUrl.openConnection();
connection.setRequestProperty("Content-Disposition", "attachment; filename=\"data.html\"");
connection.setRequestProperty("Content-Type", "application/octet-stream");
connection.setRequestMethod("POST");
connection.setConnectTimeout(60000);
connection.setReadTimeout(60000);
connection.setDoOutput(true);

/* write request */
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(htmlContent);
writer.flush();
writer.close();

/* read response */
String responseMessage = connection.getResponseMessage();
logger.info("responseMessage: " + responseMessage);

int statusCode = connection.getResponseCode();
logger.info("statusCode: " + statusCode);

if (statusCode == HttpURLConnection.HTTP_OK) {
    logger.info("HTTP status code OK");
    // parse output

    InputStream is = connection.getInputStream();

    buffer = new ByteArrayOutputStream();

    int nRead;
    byte[] data = new byte[16384];

    while ((nRead = is.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }

    buffer.flush();

    byte[] attachmentData = buffer.toByteArray();

    Multipart content = new MimeMultipart();

    ...

    MimeBodyPart attachment = new MimeBodyPart();
    InputStream attachmentDataStream = new ByteArrayInputStream(attachmentData);
    attachment.setFileName("filename-" + Long.toHexString(Double.doubleToLongBits(Math.random())) + ".pdf");
    attachment.setContent(attachmentDataStream, "application/pdf");
    content.addBodyPart(attachment);
    ...
}
DFB
  • 861
  • 10
  • 25

1 Answers1

0

You can easily push HTML string as file. I do not have JAVA example but the C# demo will give you right path.

using System;
using System.IO;
using System.Net.Http;
using System.Text;

class MainClass {
  public static void Main (string[] args) {
    var url = new Uri("https://v2.convertapi.com/html/to/pdf?download=attachment&secret=<YourSecret>");
    var htmlString = "<!doctype html><html lang=en><head><meta charset=utf-8><title>ConvertAPI test</title></head><body>This page is generated from HTML string.</body></html>";
    var content = new StringContent(htmlString, Encoding.UTF8, "application/octet-stream");
    content.Headers.Add("Content-Disposition", "attachment; filename=\"data.html\"");
    using (var resultFile = File.OpenWrite(@"C:\Path\to\result\file.pdf"))
    {
        new HttpClient().PostAsync(url, content).Result.Content.CopyToAsync(resultFile).Wait();
    }
  }
}
Tomas
  • 17,551
  • 43
  • 152
  • 257
  • thanks for your help. Is there a GET API also? That'll make things much simpler and I won't have to change my code much. Otherwise, I'll update the code to work with POST. – DFB May 21 '18 at 02:17
  • With the GET method it is uncommon to send content body. Only "legal" way to pass information in GET request is through query parameters, but URL length is restricted in length. So for now only way is to use POST or if you have HTML file publicly accessible please use this: https://www.convertapi.com/doc/virtual-file-server – Jonas May 21 '18 at 06:10
  • I managed to get a response code 200. Now trying to figure out how to parse the output. I'm updated my original post with the code so far. – DFB May 21 '18 at 16:41
  • I have managed to make it work and update the code. I'm still having problems with the final PDF output with content not laying out as expected. I'll work directly with convertapi support to get it resolved. Thanks for your help. – DFB May 21 '18 at 17:58