Java 如何使用ConvertAPI将HTML字符串转换为PDF(无物理文件)

Java 如何使用ConvertAPI将HTML字符串转换为PDF(无物理文件),java,c#,convertapi,Java,C#,Convertapi,以前我使用的是http://do.convertapi.com/Web2Pdf使用Java通过简单的GET请求(而不是POST)将HTML字符串转换为PDF。使用curl参数传递整个内容 然而,该API最近似乎已经停止工作。我正在尝试转移到https://v2.convertapi.com/web/to/pdf但是我找不到一个使用GET或POST的新API来实现相同功能的示例 有人能提供一个使用Java发出GET或POST请求的示例吗 更新:我已经设法使它工作 private static fi

以前我使用的是
http://do.convertapi.com/Web2Pdf
使用Java通过简单的GET请求(而不是POST)将HTML字符串转换为PDF。使用
curl
参数传递整个内容

然而,该API最近似乎已经停止工作。我正在尝试转移到
https://v2.convertapi.com/web/to/pdf
但是我找不到一个使用GET或POST的新API来实现相同功能的示例

有人能提供一个使用Java发出GET或POST请求的示例吗


更新:我已经设法使它工作

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);
    ...
}

您可以轻松地将HTML字符串作为文件推送。我没有JAVA示例,但C#演示将为您提供正确的路径

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();
    }
  }
}
使用系统;
使用System.IO;
使用System.Net.Http;
使用系统文本;
类主类{
公共静态void Main(字符串[]args){
var url=新Uri(“https://v2.convertapi.com/html/to/pdf?download=attachment&secret=");
var htmlString=“ConvertAPI test此页面是从HTML字符串生成的。”;
var content=新的StringContent(htmlString,Encoding.UTF8,“应用程序/八位字节流”);
添加(“内容处置”、“附件;文件名=\”data.html\”);
使用(var resultFile=File.OpenWrite(@“C:\Path\to\result\File.pdf”))
{
新建HttpClient().PostAsync(url,内容).Result.content.CopyToAsync(resultFile.Wait();
}
}
}

谢谢您的帮助。还有一个getapi吗?这将使事情变得更简单,而且我不必对代码做太多更改。否则,我将更新代码以使用POST。使用GET方法发送内容正文是不常见的。GET请求中传递信息的唯一“合法”方式是通过查询参数,但URL长度受到长度限制。所以现在唯一的方法是使用POST,或者如果你们有公开访问的HTML文件,请使用这个:我设法得到一个响应代码200。现在,我们正试图找出如何解析输出。到目前为止,我已经用代码更新了我的原始帖子。我已经设法使它工作并更新了代码。我仍然对最终的PDF输出有问题,因为内容没有按照预期的方式排列。我将直接与convertapi支持部门合作解决这个问题。谢谢你的帮助。