I'm currently using org.docx4j:docx4j-JAXB-ReferenceImpl:8.3.1 to generate a document replacing variables.
I fetch the template (with placeholders) from a google drive using the export link for DOCX.
- Code: Select all
public InputStream getDocumentByTitle(String title, String loggedInEmail) {
try {
credential = createGoogleApiService("drive", loggedInEmail);
Drive service = new Drive.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(applicationNames.get("drive"))
.build();
Drive.Files.List files = service.files().list()
.setQ("mimeType != 'application/vnd.google-apps.folder' and trashed = false and 'xxx' in parents and name = '" + title + "'")
.setCorpora("allDrives")
.setSupportsTeamDrives(true)
.setIncludeTeamDriveItems(true)
.setFields(
"files(name, webViewLink, iconLink, exportLinks)");
FileList documents = files.execute();
if (documents != null && !documents.isEmpty() && !documents.getFiles().isEmpty()) {
File file = documents.getFiles().get(0);
ArrayMap exportLinks = (ArrayMap) file.get("exportLinks");
if (exportLinks != null) {
String url = (String)exportLinks.get(DOCX_MIME);
if (url != null) {
HttpRequest req = service.getRequestFactory().buildGetRequest(
new GenericUrl(url));
HttpResponse res = req.execute();
if (res.getStatusCode() == 200) {
return res.getContent();
}
}
}
return null;
}
}catch(Exception ex){
log.info("Error occurred while accessing Google API");
return null;
}
return null;
}
I then generate the doc and upload it back to google as a GDOC.
- Code: Select all
private void upload(OpcPackage wordMLPackage, String title, String loggedInEmail) throws Exception {
GoogleAppsUtils utils = new GoogleAppsUtils();
String folderId = "xxx";
com.google.api.services.drive.model.File fileMetadata = new com.google.api.services.drive.model.File();
fileMetadata.setName(title);
fileMetadata.setParents(Collections.singletonList(folderId));
fileMetadata.setMimeType(GDOC_MIME);
InputStream inputStream = new ByteArrayInputStream(getDocxAsByteArray(wordMLPackage));
File file = new File("/tmp/output.txt");
Files.copy(inputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
utils.uploadToDrive(fileMetadata, new FileContent(null, file), loggedInEmail);
}
Here is a portion of the template being used
https://docs.google.com/document/d/1JcV ... sp=sharing
And here is the generated doc
https://docs.google.com/document/d/1q1k ... sp=sharing
When you click on the hyperlink text it the template it finds the heading, but not in the generated document.
Thanking you in advance!!