Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print Invoice Directly Using Print Button #785

Closed
hafeesk opened this issue Dec 10, 2023 · 13 comments · Fixed by #1086
Closed

Print Invoice Directly Using Print Button #785

hafeesk opened this issue Dec 10, 2023 · 13 comments · Fixed by #1086
Labels
enhancement New feature or request

Comments

@hafeesk
Copy link

hafeesk commented Dec 10, 2023

Now we can not print invoice directly,we need to download pdf and print .Print Directly From Print Button

@hafeesk hafeesk changed the title Print Directly Usinf Print Button Print Invoice Directly Using Print Button Dec 10, 2023
@mildred mildred added the enhancement New feature or request label Dec 20, 2023
@mildred
Copy link
Contributor

mildred commented Dec 20, 2023

This requires more cooperation with the system, hence this is probably not trivial.

@dvodev
Copy link

dvodev commented Jan 15, 2024

I have added this functionality locally

@Sunitsarode
Copy link

I have added this functionality locally

Hi can you please contribute your code to main branch. It is most required facility

@Isaac-GC Isaac-GC moved this from Planned to In-Progress in Frappe Books - Feature/Bugs RoadMap Feb 2, 2024
@Elanngo-Madheswaran
Copy link

Is this still working in progress or left behind ?

@Elanngo-Madheswaran
Copy link

@dvodev could you tell me how did you do it locally so that I could do that , even if it unstable no problem

@Elanngo-Madheswaran
Copy link

Elanngo-Madheswaran commented Sep 5, 2024

i changed the saveHtmlasPdf.ts file for now I havent worked on it much but it says you to save the pdf and when you click the save button it doesnt save and just open print window,

this is the changed code , I dont know if its correct or wrong but it works I guess

import { App, BrowserWindow } from 'electron';
import fs from 'fs/promises';
import path from 'path';

export async function saveHtmlAsPdf(
  html: string,
  savePath: string,
  app: App,
  width: number, // centimeters
  height: number // centimeters
): Promise<boolean> {
  /**
   * Store received html as a file in a tempdir,
   * this will be loaded into the print view
   */
  const tempRoot = app.getPath('temp');
  const filename = path.parse(savePath).name;
  const htmlPath = path.join(tempRoot, `${filename}.html`);
  await fs.writeFile(htmlPath, html, { encoding: 'utf-8' });

  const printWindow = await getInitializedPrintWindow(htmlPath, width, height);
  
  // Define print options
  const printOptions = {
    silent: false, // False to show print dialog
    printBackground: true, // Print background graphics
  };

  // Trigger the print dialog (prints to selected printer instead of saving a PDF)
  printWindow.webContents.print(printOptions, (success, errorType) => {
    if (!success) {
      console.error(`Printing failed: ${errorType}`);
    }
  });

  // Close the hidden window after printing
  printWindow.on('closed', () => {
    printWindow.destroy();
  });

  // Clean up the temporary HTML file
  await fs.unlink(htmlPath);

  return true;
}

async function getInitializedPrintWindow(
  printFilePath: string,
  width: number,
  height: number
) {
  const printWindow = new BrowserWindow({
    width: Math.floor(width * 28.333333), // pixels
    height: Math.floor(height * 28.333333), // pixels
    show: false, // Window is hidden during printing
  });

  await printWindow.loadFile(printFilePath);
  return printWindow;
}

@xcode-ae
Copy link

xcode-ae commented Sep 29, 2024

hello,
this feature is really very important and i'm also looking for this option, "print without saving as PDF"

i have tried your code and it's working fine but still showing for saving as PDF but after that you are right no pdf has been saved only redirect me to popup for printing which is very good for now. i hope we can continue on it

@dvodev
Copy link

dvodev commented Sep 29, 2024 via email

@Elanngo-Madheswaran
Copy link

Hi @dvodev,

I wanted to let you know that the fix I implemented is just a temporary solution for your local system. I understand that it won't be merged into the main branch. If I intended for it to be merged, I would have made more comprehensive changes, including altering the buttons and other elements.

Thank you for responding though

@Elanngo-Madheswaran
Copy link

hello, this feature is really very important and i'm also looking for this option, "print without saving as PDF"

i have tried your code and it's working fine but still showing for saving as PDF but after that you are right no pdf has been saved only redirect me to popup for printing which is very good for now. i hope we can continue on it

i made another version of the code where it will open the temp pdf in edge browser there you can select to print or save as pdf and When you close the temp file will be automatically deleted , but the code needs to altered according to your system (the adddress of the edge browser exe) if you want i could send that too

@xcode-ae
Copy link

xcode-ae commented Oct 1, 2024

hello, this feature is really very important and i'm also looking for this option, "print without saving as PDF"
i have tried your code and it's working fine but still showing for saving as PDF but after that you are right no pdf has been saved only redirect me to popup for printing which is very good for now. i hope we can continue on it

i made another version of the code where it will open the temp pdf in edge browser there you can select to print or save as pdf and When you close the temp file will be automatically deleted , but the code needs to altered according to your system (the adddress of the edge browser exe) if you want i could send that too

Sounds good, and I appreciate your help and support. I was already thinking that since Frappe Books uses a web version, why not add this feature for people who prefer not to save the PDF, helping them conserve storage as the app is used locally.
BTW im using Macbook and chrome browser and already there is an option for saving or printing by itself and if you share the code it will be great too !

@Elanngo-Madheswaran
Copy link

Elanngo-Madheswaran commented Oct 2, 2024

if you share the code it will be great too !

import { App } from 'electron';
import fs from 'fs/promises';
import path from 'path';
import { exec } from 'child_process';
import { promisify } from 'util';

const execPromise = promisify(exec);

export async function saveHtmlAsPdf(
  html: string,
  savePath: string,
  app: App,
  width: number, // centimeters
  height: number // centimeters
): Promise<boolean> {
  try {
    // Store HTML as a file in a temp directory
    const tempRoot = app.getPath('temp');
    const filename = path.parse(savePath).name;
    const htmlPath = path.join(tempRoot, `${filename}.html`);
    await fs.writeFile(htmlPath, html, { encoding: 'utf-8' });

    // Path to Edge executable
    const edgePath = `C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe`; // edit the path according to your computer

    // Command to open Edge with the print preview for the HTML file
    const command = `"${edgePath}" --print --no-margins "${htmlPath}"`;

    // Execute the command to open Edge
    await execPromise(command);

    // Clean up the temporary HTML file
    // Use a timeout to ensure Edge has enough time to open before deleting the file
    setTimeout(async () => {
      try {
        await fs.unlink(htmlPath);
      } catch (error) {
        if (error instanceof Error) {
          console.error(`Failed to delete temp HTML file: ${error.message}`);
        } else {
          console.error('Failed to delete temp HTML file: Unknown error');
        }
      }
    }, 5000); // 5 seconds delay to ensure Edge has time to process

    return true;
  } catch (error) {
    if (error instanceof Error) {
      console.error(`Failed to save HTML as PDF: ${error.message}`);
    } else {
      console.error('Failed to save HTML as PDF: Unknown error');
    }
    return false;
  }
}

You can edit this code according to your computer file arrangement

@xcode-ae
Copy link

xcode-ae commented Oct 3, 2024

if you share the code it will be great too !

import { App } from 'electron';
import fs from 'fs/promises';
import path from 'path';
import { exec } from 'child_process';
import { promisify } from 'util';

const execPromise = promisify(exec);

export async function saveHtmlAsPdf(
  html: string,
  savePath: string,
  app: App,
  width: number, // centimeters
  height: number // centimeters
): Promise<boolean> {
  try {
    // Store HTML as a file in a temp directory
    const tempRoot = app.getPath('temp');
    const filename = path.parse(savePath).name;
    const htmlPath = path.join(tempRoot, `${filename}.html`);
    await fs.writeFile(htmlPath, html, { encoding: 'utf-8' });

    // Path to Edge executable
    const edgePath = `C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe`; // edit the path according to your computer

    // Command to open Edge with the print preview for the HTML file
    const command = `"${edgePath}" --print --no-margins "${htmlPath}"`;

    // Execute the command to open Edge
    await execPromise(command);

    // Clean up the temporary HTML file
    // Use a timeout to ensure Edge has enough time to open before deleting the file
    setTimeout(async () => {
      try {
        await fs.unlink(htmlPath);
      } catch (error) {
        if (error instanceof Error) {
          console.error(`Failed to delete temp HTML file: ${error.message}`);
        } else {
          console.error('Failed to delete temp HTML file: Unknown error');
        }
      }
    }, 5000); // 5 seconds delay to ensure Edge has time to process

    return true;
  } catch (error) {
    if (error instanceof Error) {
      console.error(`Failed to save HTML as PDF: ${error.message}`);
    } else {
      console.error('Failed to save HTML as PDF: Unknown error');
    }
    return false;
  }
}

You can edit this code according to your computer file arrangement

Awesome, thank you very much !

@akshayitzme akshayitzme linked a pull request Jan 30, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

Successfully merging a pull request may close this issue.

7 participants