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.