use of com.lowagie.text.pdf.PdfCopy in project ofbiz-framework by apache.
the class CompDocServices method renderCompDocPdf.
public static Map<String, Object> renderCompDocPdf(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Locale locale = (Locale) context.get("locale");
String rootDir = (String) context.get("rootDir");
String webSiteId = (String) context.get("webSiteId");
String https = (String) context.get("https");
Delegator delegator = dctx.getDelegator();
String contentId = (String) context.get("contentId");
String contentRevisionSeqId = (String) context.get("contentRevisionSeqId");
try {
List<EntityCondition> exprList = new LinkedList<EntityCondition>();
exprList.add(EntityCondition.makeCondition("contentIdTo", EntityOperator.EQUALS, contentId));
exprList.add(EntityCondition.makeCondition("contentAssocTypeId", EntityOperator.EQUALS, "COMPDOC_PART"));
exprList.add(EntityCondition.makeCondition("rootRevisionContentId", EntityOperator.EQUALS, contentId));
if (UtilValidate.isNotEmpty(contentRevisionSeqId)) {
exprList.add(EntityCondition.makeCondition("contentRevisionSeqId", EntityOperator.LESS_THAN_EQUAL_TO, contentRevisionSeqId));
}
List<GenericValue> compDocParts = EntityQuery.use(delegator).select("rootRevisionContentId", "itemContentId", "maxRevisionSeqId", "contentId", "dataResourceId", "contentIdTo", "contentAssocTypeId", "fromDate", "sequenceNum").from("ContentAssocRevisionItemView").where(exprList).orderBy("sequenceNum").filterByDate().queryList();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document = new Document();
document.setPageSize(PageSize.LETTER);
PdfCopy writer = new PdfCopy(document, baos);
document.open();
for (GenericValue contentAssocRevisionItemView : compDocParts) {
String thisDataResourceId = contentAssocRevisionItemView.getString("dataResourceId");
GenericValue dataResource = EntityQuery.use(delegator).from("DataResource").where("dataResourceId", thisDataResourceId).queryOne();
String inputMimeType = null;
if (dataResource != null) {
inputMimeType = dataResource.getString("mimeTypeId");
}
byte[] inputByteArray = null;
PdfReader reader = null;
if (inputMimeType != null && "application/pdf".equals(inputMimeType)) {
ByteBuffer byteBuffer = DataResourceWorker.getContentAsByteBuffer(delegator, thisDataResourceId, https, webSiteId, locale, rootDir);
inputByteArray = byteBuffer.array();
reader = new PdfReader(inputByteArray);
} else if (inputMimeType != null && "text/html".equals(inputMimeType)) {
ByteBuffer byteBuffer = DataResourceWorker.getContentAsByteBuffer(delegator, thisDataResourceId, https, webSiteId, locale, rootDir);
inputByteArray = byteBuffer.array();
String s = new String(inputByteArray, "UTF-8");
Debug.logInfo("text/html string:" + s, module);
continue;
} else if (inputMimeType != null && "application/vnd.ofbiz.survey.response".equals(inputMimeType)) {
String surveyResponseId = dataResource.getString("relatedDetailId");
String surveyId = null;
String acroFormContentId = null;
GenericValue surveyResponse = null;
if (UtilValidate.isNotEmpty(surveyResponseId)) {
surveyResponse = EntityQuery.use(delegator).from("SurveyResponse").where("surveyResponseId", surveyResponseId).queryOne();
if (surveyResponse != null) {
surveyId = surveyResponse.getString("surveyId");
}
}
if (UtilValidate.isNotEmpty(surveyId)) {
GenericValue survey = EntityQuery.use(delegator).from("Survey").where("surveyId", surveyId).queryOne();
if (survey != null) {
acroFormContentId = survey.getString("acroFormContentId");
if (UtilValidate.isNotEmpty(acroFormContentId)) {
// TODO: is something supposed to be done here?
}
}
}
if (surveyResponse != null) {
if (UtilValidate.isEmpty(acroFormContentId)) {
// Create AcroForm PDF
Map<String, Object> survey2PdfResults = dispatcher.runSync("buildPdfFromSurveyResponse", UtilMisc.toMap("surveyResponseId", surveyId));
if (ServiceUtil.isError(survey2PdfResults)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentSurveyErrorBuildingPDF", locale), null, null, survey2PdfResults);
}
ByteBuffer outByteBuffer = (ByteBuffer) survey2PdfResults.get("outByteBuffer");
inputByteArray = outByteBuffer.array();
reader = new PdfReader(inputByteArray);
} else {
// Fill in acroForm
Map<String, Object> survey2AcroFieldResults = dispatcher.runSync("setAcroFieldsFromSurveyResponse", UtilMisc.toMap("surveyResponseId", surveyResponseId));
if (ServiceUtil.isError(survey2AcroFieldResults)) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentSurveyErrorSettingAcroFields", locale), null, null, survey2AcroFieldResults);
}
ByteBuffer outByteBuffer = (ByteBuffer) survey2AcroFieldResults.get("outByteBuffer");
inputByteArray = outByteBuffer.array();
reader = new PdfReader(inputByteArray);
}
}
} else {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentMimeTypeNotSupported", locale));
}
if (reader != null) {
int n = reader.getNumberOfPages();
for (int i = 0; i < n; i++) {
PdfImportedPage pg = writer.getImportedPage(reader, i + 1);
writer.addPage(pg);
}
}
}
document.close();
ByteBuffer outByteBuffer = ByteBuffer.wrap(baos.toByteArray());
Map<String, Object> results = ServiceUtil.returnSuccess();
results.put("outByteBuffer", outByteBuffer);
return results;
} catch (GenericEntityException e) {
return ServiceUtil.returnError(e.toString());
} catch (IOException | DocumentException | GeneralException e) {
Debug.logError(e, "Error in CompDoc operation: ", module);
return ServiceUtil.returnError(e.toString());
}
}
use of com.lowagie.text.pdf.PdfCopy in project jaffa-framework by jaffa-projects.
the class MultiFormPrintEngine method mergePdf.
/**
* Merge a list of generated Pdf Documents together
* @param documents
* @throws java.io.IOException
* @throws com.lowagie.text.DocumentException
* @return byte[]
*/
public static byte[] mergePdf(List<byte[]> documents) throws IOException, DocumentException {
int pageOffset = 0;
ArrayList master = new ArrayList();
ByteArrayOutputStream output = new ByteArrayOutputStream();
Document document = null;
PdfCopy writer = null;
boolean first = true;
for (Iterator<byte[]> it = documents.iterator(); it.hasNext(); ) {
// we create a reader for a certain document
PdfReader reader = new PdfReader(it.next());
reader.consolidateNamedDestinations();
// we retrieve the total number of pages
int n = reader.getNumberOfPages();
List bookmarks = SimpleBookmark.getBookmark(reader);
if (bookmarks != null) {
if (pageOffset != 0)
SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
master.addAll(bookmarks);
}
pageOffset += n;
if (first) {
first = false;
// step 1: creation of a document-object
document = new Document(reader.getPageSizeWithRotation(1));
// step 2: we create a writer that listens to the document
writer = new PdfCopy(document, output);
// step 3: we open the document
document.open();
}
// step 4: we add content
PdfImportedPage page;
for (int i = 0; i < n; ) {
++i;
page = writer.getImportedPage(reader, i);
writer.addPage(page);
}
PRAcroForm form = reader.getAcroForm();
if (form != null)
writer.copyAcroForm(reader);
}
if (master.size() > 0)
writer.setOutlines(master);
// step 5: we close the document
if (document != null)
document.close();
return output.toByteArray();
}
Aggregations