huysentruitw / print-it

Windows service for printing PDF files to a local or network printer in the background
MIT License
84 stars 43 forks source link

Can you provide examples of API calls? #7

Closed tasksljz closed 3 years ago

tasksljz commented 3 years ago

Any language like PHP will do. I always fail when I debug /api/print/from-pdf. Thank you!

huysentruitw commented 3 years ago

Does this gist help?

huysentruitw commented 3 years ago

A multipart form-post should also work:

<form action="http://localhost:7000/print/from-pdf" method="post" enctype="multipart/form-data">
  <input type="text" name="printerPath">
  <input type="file" name="pdfFile" accept=".pdf">
  <input type="submit" value="Submit">
</form>
tasksljz commented 3 years ago

Does this gist help?

I rarely use C#, I will try.

tasksljz commented 3 years ago

A multipart form-post should also work:


<form action="http://localhost:7000/print/from-pdf" method="post" enctype="multipart/form-data">

  <input type="text" name="printerPath">

  <input type="file" name="pdfFile" accept=".pdf">

  <input type="submit" value="Submit">

</form>

The server is based on Apache 2 and can be sent to the printer, but it prints out blank pages. Printer: EPSON ET-2650 Series.

huysentruitw commented 3 years ago

Can you provide a minimal PHP snippet and pass me a sample PDF so I can test it here to see what's going on?

tasksljz commented 3 years ago

Can you provide a minimal PHP snippet and pass me a sample PDF so I can test it here to see what's going on?

if(!empty($_POST) || !empty($_FILES)) {
    var_dump($_POST);
    var_dump($_FILES);
    exit;
}

function buildDataFiles($boundary, $fields, $files){
    $data = '';
    $eol = "\r\n";
    $delimiter = '-------------' . $boundary;
    foreach ($fields as $name => $content) {
        $data .= "--" . $delimiter . $eol
            . 'Content-Disposition: form-data; name="' . $name . "\"".$eol.$eol
            . $content . $eol;
    }
    foreach ($files as $name => $content) {
        $data .= "--" . $delimiter . $eol
            . 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $name . '"' . $eol
            . 'Content-Transfer-Encoding: binary'.$eol
            ;
        $data .= $eol;
        $data .= $content . $eol;
    }
    $data .= "--" . $delimiter . "--".$eol;
    return $data;
}
$fields = ["PrinterPath" => "Microsoft Print to PDF"];
$filenames = ["PdfFile" => ".\\printer\\3004-01.pdf"];
$files = [];
foreach ($filenames as $name => $filePath) {
    $files[$name] = file_get_contents($filePath);
}
$url = "http://localhost:7000/print/from-pdf";
$curl = curl_init();
$boundary = uniqid();
$delimiter = '-------------' . $boundary;
$postData = buildDataFiles($boundary, $fields, $files);
curl_setopt_array($curl, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $postData,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_HTTPHEADER => [
        "Content-Type: multipart/form-data; boundary=" . $delimiter,
        "Content-Length: " . strlen($postData)
    ],
]);
$response = curl_exec($curl);
curl_close($curl);
if ($err = curl_error($curl)) var_dump($err);
var_dump($response);

The source code always fails to compile under vs2019. Can you use dotnet to compile your project?

I am trying to use only the printed part of the code. I imported PrintIt.Core/Pdfium/ and PrintIt.Core/Pdfium/ into the project, using dotnet, no errors were reported during the compilation process, but it did not work normally, and the document would not be sent to the printer.

  using (FileStream pdfStream = File.Open("test.pdf", FileMode.Open))
  {
      PdfDocument PdfDocument = PdfDocument.Open(pdfStream);
      PrintDocument printDocument = new PrintDocument();
      printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
      PrintState state = PrintStateFactory.Create(PdfDocument);
      printDocument.PrintPage += (_, e) => PrintDocumentOnPrintPage(e, state);
      printDocument.Print();
  }

  private void PrintDocumentOnPrintPage(PrintPageEventArgs e, PrintState state)
  {
      var destinationRect = new RectangleF(
          x: e.Graphics.VisibleClipBounds.X * e.Graphics.DpiX / 100.0f,
          y: e.Graphics.VisibleClipBounds.Y * e.Graphics.DpiY / 100.0f,
          width: e.Graphics.VisibleClipBounds.Width * e.Graphics.DpiX / 100.0f,
          height: e.Graphics.VisibleClipBounds.Height * e.Graphics.DpiY / 100.0f);
      using PdfPage page = state.Document.OpenPage(state.CurrentPageIndex);
      page.RenderTo(e.Graphics, destinationRect);
      e.HasMorePages = state.AdvanceToNextPage();
  }

Sorry, my English is not very good. Hope you can understand what I mean, thank you!

tasksljz commented 3 years ago

The ghostscript DLL has been used to achieve the same function.