From your description, it seems that custom code work well when you preview the report. While it would be skipped when the report is exported to PDF. After testing the custom code in my environment,I find that the custom code will run again when exporting
the report, so when we run the report on Report Server and export it to PDF, the custom code will be run twice, the value will be counted twice.
Based on my research, I find that we can delete the “Shared” in the first row of the custom code to fix this issue. Then the custom code will works well on Report Server.
If there are any misunderstanding, please elaborate the issue for further investigation.