1

Please bear in mind that this code is running in an ISAPI module, and we would rather not mess around with changing which user the application logs in as, so the usual way of using the Printer classes is not likely to be an option.

I have the following code:

procedure WriteFileToPrinter(const APrinterURL, AFilename: string);
var
  LHandle, LWritten: Cardinal;
  LFileStream: TStream;
  LMemoryStream: TMemoryStream;
begin
  LHandle := CreateFile(PChar(APrinterURL), GENERIC_WRITE, 0, nil, OPEN_ALWAYS, FILE_FLAG_WRITE_THROUGH, 0);
  if LHandle > 0 then
  try
    LFileStream := TFileStream.Create(AFilename, fmOpenRead or fmShareDenyNone);
    try
      LMemoryStream := TMemoryStream.Create;
      try
        LFileStream.Position := 0;
        LMemoryStream.CopyFrom(LFileStream, LFileStream.Size);
        WriteFile(LHandle, LMemoryStream.Memory^, LMemoryStream.Size, LWritten, nil);
      finally
        LMemoryStream.Free;
      end;
    finally
      LFileStream.Free;
    end;
  finally
    CloseHandle(LHandle);
  end;
end;

Which successfully sends the file to the printer, and while not all printers tested support this, there is one difference on a printer that does: the file is a PDF, and all the bold fonts are ignored. I have verified that the PDF actually has bold fonts in it, and printing to the same printer using Acrobat Reader prints as expected.

The printer in question is a Brother MFC-9330CDW. It may be that it's just this particular printer that is having the issue, however I thought I would ask in case it happens with other printers, and whether there might be an alternative method.

Dave Nottage
  • 3,411
  • 1
  • 20
  • 57
  • 1
    You don't need the `TFileStream` if you use `TMemoryStream`, as `TMemoryStream` has a `LoadFromFile()` method. Or, you could get rid of the `TMemoryStream` and instead wrap `LHandle` in a `THandleStream` and then have it `CopyFrom()` the `TFileStream` – Remy Lebeau Feb 05 '18 at 23:05
  • Thanks for the tip!.. I didn't even realize that TMemoryStream had LoadFromFile. – Dave Nottage Feb 05 '18 at 23:07
  • 1
    Yes, it's the memory stream that should be abandoned here. As a sweeping generalisation, the majority of uses of memory streams by delphi programmers are needless. Here you should simple copy from one stream to another without any middle man. As far as the question goes, we can't help from here. Start by gathering evidence. Look at what drivers are involved. Check with different logged on users. Connect to different machines. Work the problem space. – David Heffernan Feb 06 '18 at 08:11
  • @DavidHeffernan If you have any insights as to how drivers might affect the code described (given that CreateFile is being used), I'm all ears – Dave Nottage Feb 06 '18 at 08:52
  • That's not really how Stack Overflow works. This site is for specific questions with detail sufficient to provide detailed answers. If you want speculation and broad ideas you should look elsewhere for help. For instance G+ Delphi devs. – David Heffernan Feb 06 '18 at 08:54
  • I'm not asking for speculation: you did, regarding drivers. If you know something specific that connects drivers to this issue, then please enlighten me – Dave Nottage Feb 06 '18 at 08:57
  • I've voted to close the question. You asked for speculation. In the question, and in the comments. – David Heffernan Feb 06 '18 at 09:08
  • In the question, perhaps, so that is fair enough. Definitely not in the comments: I asked for specific connection. You made the soeculation – Dave Nottage Feb 06 '18 at 09:15
  • *If you have any insights as to how drivers might affect the code described* is asking for speculation in my view. Anyway, this question doesn't fit here. – David Heffernan Feb 06 '18 at 09:18
  • What was the point of: *Look at what drivers are involved*, then? Unless you had something specific in mind, you were just speculating. – Dave Nottage Feb 06 '18 at 09:21
  • I was certainly speculating. Which is why I put it in a comment. But my close vote is because the question doesn't fit, and can't be answered within the framework of this site. If you are unsure on the type of questions that fit here, please visit the [help]. – David Heffernan Feb 06 '18 at 09:26

0 Answers0