groupdocs-free-consulting / projects

0 stars 0 forks source link

I want to create/build #37

Open gustavo-herrera-cabrera opened 1 year ago

gustavo-herrera-cabrera commented 1 year ago

Describe your free consulting project requirements briefly

Hello, right now I'm working in a requirement for a project, and I'm trying to review if this product is factible to use in it.

What we need to do is:

Basically, a client that uses our service would be able to provide some information and upload documents to the system, and the system would return a PDF of the document, parsed with the headers.

In the case of a Word document, it should set the headers of every page. In a PowerPoint presentation, it sould add row in every slide In a Excel sheet, add a row in the beginning.

Would it be possible to do something like that using GroupDocs?

Which platform you're working on -- operating system, development environment etc.

Platform: Node 16 OS: Windows 10/11

Input files

ART_TRAZADOR_ACT.docx check_list_BIOMBOS_Y_PANTALLAS_ACUSTICAS.docx

Sample outputs. they look kinda bad, but they encapsulate the intent of what we need:

output1.docx output2.docx

atirtahirgroupdocs commented 1 year ago

@gustavo-herrera-cabrera

Would it be possible to do something like that using GroupDocs?

Are you considering using NodeJS to accomplish this task? Have you checked out our GroupDocs.Editor cloud SDK for NodeJS?

gustavo-herrera-cabrera commented 1 year ago

@gustavo-herrera-cabrera

Would it be possible to do something like that using GroupDocs?

Are you considering using NodeJS to accomplish this task? Have you checked out our GroupDocs.Editor cloud SDK for NodeJS?

Yes, we even made some code based of this guide.

const {
  readFileSync,
} = require("fs");
const {
  FileInfo,
  WordProcessingLoadOptions,
  LoadRequest,
  LoadResult,
  DownloadFileRequest,
  UploadFileRequest,
  WordProcessingSaveOptions,
  SaveRequest,
} = require("groupdocs-editor-cloud/lib/model");
const {
  FileApi,
  EditApi,
} = require("./src/config/groupdoc-editor-cloud");

/**
 * Uploads the file to GroupDocs's storage.
 * @param {string} localPath Path to the file on the local machine.
 * @param {string} storagePath Route to the file in GroupDocs's storage.
 * @returns
 */
const uploadFile = async (
  localPath = "./input.docx",
  storagePath = "input.docx"
) => {
  console.info("Uploading file...");

  const document = readFileSync(localPath);
  const result = await FileApi.uploadFile({
    file: document,
    path: storagePath,
    storageName: "test-storage",
  });

  console.info("File uploaded successfully.");
  console.info(JSON.stringify(result, null, 2));

  return result;
};

/**
 * Loads the file from GroupDocs storage into a `Buffer`.
 * @param {string} storagePath Route to the file in GroupDocs's storage.
 */
const loadFile = async (storagePath = "input.docx") => {
  console.info("Loading file...");

  const fileInfo = new FileInfo({
    filePath: storagePath,
    storageName: "test-storage",
  });

  const loadOptions = new WordProcessingLoadOptions({
    fileInfo,
    outputPath: "output.docx",
  });

  const response = await EditApi.load(
    new LoadRequest(loadOptions)
  );

  console.info("File loaded successfully.");
  console.info(JSON.stringify(response, null, 2), "\n");

  console.info("Starting download...");
  const buffer = await FileApi.downloadFile(
    new DownloadFileRequest(
      response.htmlPath,
      "test-storage"
    )
  );
  console.info("Download complete.");
  // console.info(buffer.toString("utf8"));

  return { loadResponse: response, buffer, fileInfo };
};

/**
 * @param {Buffer} buffer Buffer of the file.
 * @returns {Promise<Buffer>}
 */
const editFile = async (buffer) => {
  let htmlString = buffer.toString("utf-8");

  // do something with string, but what?

  return Buffer.from(htmlString, "utf-8");
};

/**
 * @param {Buffer} buffer
 * @param {LoadResult} loadResult
 * @param {FileInfo} fileInfo
 */
const saveEdits = async (
  buffer,
  { htmlPath, resourcesPath },
  fileInfo,
  outputPath = "output.docx"
) => {
  console.info("Uploading edits...");
  const fileUploadResult = await FileApi.uploadFile(
    new UploadFileRequest(htmlPath, buffer, "test-storage")
  );
  console.info("Edits uploaded successfully.");
  console.info(JSON.stringify(fileUploadResult, null, 2));

  const save = new WordProcessingSaveOptions();
  save.fileInfo = fileInfo;
  save.outputPath = outputPath;
  save.htmlPath = htmlPath;
  save.resourcesPath = resourcesPath;

  const saveRequest = new SaveRequest(save);

  console.info("Transforming into docx...");
  const documentResult = await EditApi.save(saveRequest);
  console.info("Transformation complete.");
  console.info(JSON.stringify(documentResult, null, 2));

  return { fileUploadResult, documentResult };
};

const main = async () => {
  try {
    const storagePath = "input.docx";
    const localPath = "./input.docx";

    await uploadFile(localPath, storagePath);

    const { loadResponse, buffer, fileInfo } =
      await loadFile(storagePath);

    const editedBuffer = await editFile(buffer);

    await saveEdits(editedBuffer, loadResponse, fileInfo);
  } catch (error) {
    console.error(error);
  }
};

main();

What we're not so sure of is how would we go around modifying the HTML string to add headers into a document.

tilalahmad commented 1 year ago

@gustavo-herrera-cabrera

Modify the document, add a header (like company logos), and texts

You can use the GroupDocs.Annotation Cloud SDK for Node.js to add image and text in your documents. Please check the following documentation for details. If you face any issue or have any questions, then you may post your query in our free forum along with your source and expected document. We will guide you accordingly.

Image Annotation Watermark Annotation