如何在java中设置pdf响应的默认缩放值

时间:2016-07-10 07:20:52

标签: java rest jersey pdf-generation

我写了restful webservice,它将返回PDF文件,这个PDF将出现在浏览器的IFrame中。 这部分很好。

但是,我遇到困难的是,PDF文件在浏览器上打开,选择缩放值“自动缩放”,但我想显示此PDF,并选择缩放值“页面宽度”。 请在下面找到返回PDF的方法。

/**
* @param file
* @return Response object.
*/
private Response processRequest(final String filePath)
{

  File file = new File(filePath);

  PDPageFitDestination dest = new PDPageFitDestination();
  PDActionGoTo action = new PDActionGoTo();    
  action.setDestination(dest);

  ByteArrayOutputStream output = new ByteArrayOutputStream();

  PDDocument pd=null;
  try
  {
     pd = PDDocument.load(file);
     pd.getDocumentCatalog().setOpenAction(action);
     pd.save(output);
  }
  catch(IOException e)
  {
     e.printStackTrace();
  }
  catch(COSVisitorException e)
  {
     e.printStackTrace();
  }

  //ResponseBuilder responseBuilder = Response.ok((Object)file);

  ResponseBuilder responseBuilder = Response.ok(output.toByteArray());
  responseBuilder.header("Content-Type", "application/pdf; filename=return.pdf");
  responseBuilder.header("Content-Disposition", "inline");
  return responseBuilder.build();
  }

我认为通过提供特定于缩放值的任何标题值将返回具有缩放值“页面宽度”的PDF,但不会获得与其相关的标题。 请提供这方面的建议。

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。我需要在请求URL中使用pdf特定参数。有关详情,请转至PDFOpenParameters

相关问题